﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ProjBackEnd.DBInstance;
using ProjBackEnd.Lib;
using ProjBackEnd.Areas.Financial.Models;
using System.Reflection;
using System.Data;
using ProjBackEnd.Areas.BizProj.Models;
using ProjBackEnd.Models;
using ProjBackEnd.Areas.ExtPub.Models;

namespace ProjBackEnd.DAL
{
    /// <summary>
    /// 财务类业务逻辑
    /// </summary>
    public class financialLogic
    {
        /// <summary>
        /// 获取账单扩展信息
        /// </summary>
        /// <param name="_strCreateid">建档人</param>
        /// <param name="_strComcd">所属公司</param>
        /// <param name="_iPageSize">每页记录数</param>
        /// <param name="_iCurrPage">当前页码</param>
        /// <param name="_iBillid">账单id</param>
        /// <param name="_iCusid">结算客户id</param>
        /// <param name="_strRplb">收付费标志：R-收费；P-付费</param>
        /// <param name="_strsPid">账单状态</param>
        /// <param name="_strBilltp_inner">账单内置类别：BIZ-业务类；ADM-行政类</param>
        /// <param name="_strSettleLb">预结标志（Y-已全部做账【做账：应收应付金额=预结金额】；N-未全部做账；空值=所有）</param>
        /// <returns></returns>
        public static RespBill GetBillExtInfo(string _strCreateid, string _strComcd, int _iPageSize, int _iCurrPage, int _iBillid, int _iCusid, string _strRplb, string[] _strsPid,string _strBilltp_inner,string _strSettleLb)
        {
            RespBill respReturn = new RespBill();
            bool hasNextPage = false;
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if (_iPageSize == 0) throw new BizException("每页显示条数不能为0");
                int iRecTotal, iPageCount;
                iRecTotal = fcctx.VIEW_BIZ_BILLHEADER.Count(n => (string.IsNullOrEmpty(_strComcd) ? true : n.comcd == _strComcd)
                                                                                            && (n.billtp_inner == _strBilltp_inner)
                                                                                            && (string.IsNullOrEmpty(_strCreateid) ? true : n.createid == _strCreateid)
                                                                                            && (string.IsNullOrEmpty(_strRplb) ? true : n.rplb == _strRplb)
                                                                                            && ((_strsPid.Length==1&&_strsPid[0]=="") ? true : _strsPid.Contains(n.pid))
                                                                                            && (string.IsNullOrEmpty(_strSettleLb) ? true : (_strSettleLb == "Y" ? (n.amt_pay + n.amt_rec) == (n.amt_pre + n.amt_pay_ed + n.amt_rec_ed) : (n.amt_pay + n.amt_rec) != (n.amt_pre + n.amt_pay_ed + n.amt_rec_ed)))
                                                                                            && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                            && (_iBillid == 0 ? true : n.billid == _iBillid));
                if ((iRecTotal / _iPageSize - _iCurrPage) > 0) hasNextPage = true;
                else if ((iRecTotal / _iPageSize - _iCurrPage) == 0)
                {
                    if ((iRecTotal % _iPageSize) > 0) hasNextPage = true;
                }
                iPageCount = (iRecTotal == 0 ? 0 : iRecTotal / _iPageSize + 1);
                List<BillExtInfo> lstBillExt = fcctx.VIEW_BIZ_BILLHEADER.Where(n => (string.IsNullOrEmpty(_strComcd) ? true : n.comcd == _strComcd)
                                                                                            && (n.billtp_inner == _strBilltp_inner)
                                                                                            && (string.IsNullOrEmpty(_strCreateid) ? true : n.createid == _strCreateid)
                                                                                            && ((_strsPid.Length == 1 && _strsPid[0] == "") ? true : _strsPid.Contains(n.pid))
                                                                                            && (string.IsNullOrEmpty(_strSettleLb) ? true : (_strSettleLb == "Y" ? (n.amt_pay + n.amt_rec) == (n.amt_pre + n.amt_pay_ed + n.amt_rec_ed) : (n.amt_pay + n.amt_rec) != (n.amt_pre + n.amt_pay_ed + n.amt_rec_ed)))
                                                                                            && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                            && (_iBillid == 0 ? true : n.billid == _iBillid))
                                                                                            .OrderBy(n => n.billid)
                                                                                            .Take(_iPageSize * _iCurrPage).Skip(_iPageSize * (_iCurrPage - 1)).AsEnumerable()
                                                                                            .Select(n => new BillExtInfo
                                                                                            {
                                                                                                amt_pay = n.amt_pay,
                                                                                                amt_rec = n.amt_rec,
                                                                                                amt_pre = n.amt_pre,
                                                                                                amt_pay_ed = n.amt_pay_ed,
                                                                                                amt_rec_ed = n.amt_rec_ed,
                                                                                                billid = n.billid,
                                                                                                billtp = n.billtp,
                                                                                                billtpnm = n.billtpnm,
                                                                                                createdt = n.createdt.ToString("yyyy-MM-dd HH:mm"),
                                                                                                creator = n.creator,
                                                                                                cusid = n.cusid,
                                                                                                cusnm = n.cusnm,
                                                                                                invotp = n.invotp,
                                                                                                invotpnm = n.invotpnm,
                                                                                                optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                                pid = n.pid,
                                                                                                pidnm = n.pidnm,
                                                                                                remark = n.remark,
                                                                                                revcaseid = n.revcaseid,
                                                                                                revcasenm = n.revcasenm,
                                                                                                rplb = n.rplb,
                                                                                                taxrate = n.taxrate
                                                                                            }).ToList();
                respReturn.result = new ResultBill { billextlist = lstBillExt, hasnextpage = hasNextPage, pagecount = iPageCount, reccount = iRecTotal };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 更新账单基础信息
        /// </summary>
        /// <param name="_strMemberid">操作员id</param>
        /// <param name="_strComcd">公司代码</param>
        /// <param name="_collec">账单基础信息</param>
        /// <param name="_strBilltp_inner">账单内置类别</param>
        /// <returns></returns>
        public static RespBill UpdateBillBaseInfo(string _strMemberid,string _strComcd, BillBaseInfo _collec,string _strBilltp_inner)
        {
            RespBill respReturn = new RespBill();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                int iBillDetailCount;
                int iBillid = 0;
                switch (_collec.optlb)
                {
                    #region 新增
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        iBillid = sysLogic.GetRefNo("BILL");
                        if (iBillid == 0) throw new BizException("获取流水号失败");
                        FIN_BILLTB entityIns = new FIN_BILLTB();
                        entityIns.amt_pay = entityIns.amt_rec = entityIns.amt_pre = entityIns.amt_rec_ed = entityIns.amt_pay_ed = 0;
                        entityIns.billid = iBillid;
                        entityIns.billtp = _collec.billtp;
                        entityIns.billtp_inner = _strBilltp_inner;
                        entityIns.comcd = _strComcd;
                        entityIns.createdt = DateTime.Now;
                        entityIns.createid = _strMemberid;
                        entityIns.cusid = _collec.cusid;
                        entityIns.invotp = _collec.invotp ?? "";
                        entityIns.pid = "0";
                        entityIns.remark = _collec.remark ?? "";
                        entityIns.revcaseid = _collec.revcaseid;
                        entityIns.rplb = _collec.rplb;
                        entityIns.taxrate = _collec.taxrate;
                        fcctx.FIN_BILLTB.InsertOnSubmit(entityIns);
                        break;
                    #endregion
                    #region 更新
                    case GlobalSettings.ENTITYOPERATION_UPDATE:
                        FIN_BILLTB entityUpd = fcctx.FIN_BILLTB.SingleOrDefault(n => n.billid == _collec.billid);
                        if (entityUpd == default(FIN_BILLTB)) throw new BizException("未找到账单信息");
                        if (string.Compare(entityUpd.pid, "1") > 0) throw new BizException("账单正在审核，无法修改");
                        iBillDetailCount = fcctx.BIZ_FEE_DETAIL_fin.Count(n => n.billid == _collec.billid);
                        if (iBillDetailCount > 0)
                        {
                            if (entityUpd.cusid != _collec.cusid && _collec.cusid != 0) throw new BizException("账单已有明细，无法变更客户");
                            if (entityUpd.rplb != _collec.rplb && string.IsNullOrEmpty(_collec.rplb)) throw new BizException("账单已有明细，无法变更收付标志");
                        }
                        entityUpd.cusid = _collec.cusid == 0 ? entityUpd.cusid : _collec.cusid;
                        entityUpd.taxrate = _collec.taxrate < 0 ? entityUpd.taxrate : _collec.taxrate; ;
                        entityUpd.rplb = string.IsNullOrEmpty(_collec.rplb) ? entityUpd.rplb : _collec.rplb;
                        entityUpd.billtp = string.IsNullOrEmpty(_collec.billtp) ? entityUpd.billtp : _collec.billtp;
                        entityUpd.invotp = string.IsNullOrEmpty(_collec.invotp) ? entityUpd.invotp : _collec.invotp;
                        entityUpd.revcaseid = _collec.revcaseid == 0 ? entityUpd.revcaseid : _collec.revcaseid;
                        entityUpd.remark = _collec.remark ?? entityUpd.remark;
                        break;
                    #endregion
                    #region 删除
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        FIN_BILLTB entityDel = fcctx.FIN_BILLTB.SingleOrDefault(n => n.billid == _collec.billid);
                        if (entityDel == default(FIN_BILLTB)) throw new BizException("未找到账单信息");
                        iBillDetailCount = fcctx.BIZ_FEE_DETAIL_fin.Count(n => n.billid == _collec.billid);
                        if (iBillDetailCount > 0) throw new BizException("账单已有明细，无法删除");
                        fcctx.FIN_BILLTB.DeleteOnSubmit(entityDel);
                        break;
                    #endregion
                    default: throw new Exception("操作标识符异常");
                }
                fcctx.SubmitChanges();
                respReturn.result = new ResultBill { billextlist = new List<BillExtInfo> { new BillExtInfo { billid = iBillid, optlb = GlobalSettings.ENTITYOPERATION_NONE } }, hasnextpage = false, pagecount = 1, reccount = 1 };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFinaNullResultInfo SubmitBill(string _strMemberid,string _strComcd,BillCore _collec)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if (_collec.optlb != GlobalSettings.ENTITYOPERATION_UPDATE) throw new Exception("操作标识符异常");
                FIN_BILLTB entityUpd = fcctx.FIN_BILLTB.SingleOrDefault(n => n.billid == _collec.billid);
                if (entityUpd == default(FIN_BILLTB)) throw new BizException("未找到账单信息");
                if (entityUpd.cusid == 0) throw new BizException("未选收付费客户");
                if (entityUpd.revcaseid == 0 && _collec.revcaseid == 0) throw new BizException("未选择审批方案");
                if (string.Compare(entityUpd.pid, "1") > 0) throw new BizException("账单正在审核，无法重复提交");
                if (entityUpd.revcaseid == 0 && _collec.revcaseid != 0) entityUpd.revcaseid = _collec.revcaseid;
                List<BIZ_FEE_DETAIL_fin> lstFeeDetail = fcctx.BIZ_FEE_DETAIL_fin.Where(n => n.billid == _collec.billid).ToList();
                if (lstFeeDetail.Count == 0) throw new BizException("未添加账单明细");
                foreach(BIZ_FEE_DETAIL_fin _entity in lstFeeDetail) _entity.pid = "2";
                //****增加审批流程明细
                FIN_REVCASETB entityRevCase = fcctx.FIN_REVCASETB.SingleOrDefault(n => n.revcaseid == entityUpd.revcaseid && n.validlb == "Y");
                if (entityRevCase == default(FIN_REVCASETB)) throw new BizException("未找到有效的审批方案");
                List<FIN_REVCASE_DETAIL> lstCaseDtlSour = fcctx.FIN_REVCASE_DETAIL.Where(n => n.revcaseid == entityUpd.revcaseid).ToList();
                if(lstCaseDtlSour.Count==0) throw new BizException("未找到可用的审批方案明细");
                List<FIN_REVFLOW> lstFlow = new List<FIN_REVFLOW>();
                foreach (FIN_REVCASE_DETAIL _entity in lstCaseDtlSour)
                {
                    FIN_REVFLOW entityInsFlow = new FIN_REVFLOW
                    {
                        applyid = entityUpd.billid,
                        applytp = GlobalSettings.APPLYTP_BILL,
                        reviewer1 = _entity.reviewer1,
                        reviewer2 = _entity.reviewer2
                    };
                    entityInsFlow.revlb1 = entityInsFlow.revlb2 = "";
                    entityInsFlow.revtp = _entity.revtp;
                    entityInsFlow.sortval = _entity.sortval;
                    lstFlow.Add(entityInsFlow);
                }
                lstFlow[0].submitdt = DateTime.Now;
                lstFlow[0].revlb1 = "N";
                lstFlow[0].revlb2 = string.IsNullOrEmpty(lstFlow[0].reviewer2) ? "" : "N";
                fcctx.FIN_REVFLOW.InsertAllOnSubmit(lstFlow);
                //**********************
                entityUpd.pid = "2";
                fcctx.SubmitChanges();

                VIEW_FIN_APPROVAL_ALL entityApply = fcctx.VIEW_FIN_APPROVAL_ALL.FirstOrDefault(n => n.applyid == _collec.billid && n.applytp == GlobalSettings.APPLYTP_BILL);
                extPubWxLogic.SendWxMsgApplyReq(GlobalSettings.APPLYTP_BILL,
                                                                            _collec.billid,
                                                                            _strComcd,
                                                                            lstFlow[0].reviewer1,
                                                                            1,
                                                                            new ExtWxSendMsgData
                                                                            {
                                                                                first = new ExtWxSendMsgDataCore { value = "您有一笔申请需要审批，审批单号：#" + _collec.billid },
                                                                                keyword1 = new ExtWxSendMsgDataCore { value = entityApply.applytpnm }, //申请类型
                                                                                keyword2 = new ExtWxSendMsgDataCore { value = entityApply.creator }, //申请人
                                                                                keyword3 = new ExtWxSendMsgDataCore { value = entityApply.submitdt == null ? "" : ((DateTime)entityApply.submitdt).ToString("yyyy-MM-dd HH:mm:ss") },//申请时间
                                                                                keyword4 = new ExtWxSendMsgDataCore { value = entityApply.rplbnm + entityApply.amt_apply.ToString() } //申请事由
                                                                            });
                if(!string.IsNullOrEmpty(lstFlow[0].reviewer2))
                {
                    extPubWxLogic.SendWxMsgApplyReq(GlobalSettings.APPLYTP_BILL,
                                                                            _collec.billid,
                                                                            _strComcd,
                                                                            lstFlow[0].reviewer2,
                                                                            1,
                                                                            new ExtWxSendMsgData
                                                                            {
                                                                                first = new ExtWxSendMsgDataCore { value = "您有一笔申请需要审批，审批单号：#" + _collec.billid },
                                                                                keyword1 = new ExtWxSendMsgDataCore { value = entityApply.applytpnm },
                                                                                keyword2 = new ExtWxSendMsgDataCore { value = entityApply.creator },
                                                                                keyword3 = new ExtWxSendMsgDataCore { value = entityApply.submitdt == null ? "" : ((DateTime)entityApply.submitdt).ToString("yyyy-MM-dd HH:mm:ss") },
                                                                                keyword4 = new ExtWxSendMsgDataCore { value = entityApply.rplbnm + entityApply.amt_apply.ToString() }
                                                                            });
                }
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 为账单增删费用明细
        /// </summary>
        /// <param name="_ibillid">账单id</param>
        /// <param name="_strOptlb">操作标识符</param>
        /// <param name="_lst">费用明细列表</param>
        /// <returns></returns>
        public static RespFinaNullResultInfo AdjustBizBillDetail(int _ibillid, string _strOptlb, List<BizFeeCore> _lst)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                FIN_BILLTB entityBill = fcctx.FIN_BILLTB.SingleOrDefault(n => n.billid == _ibillid && n.billtp_inner == GlobalSettings.FNTP_BIZ);
                if (entityBill == default(FIN_BILLTB)) throw new BizException("未找到账单信息");
                if (string.Compare(entityBill.pid, "1") > 0) throw new BizException("账单已有审批流程，无法调整明细");
                List<int> lstBillDetailid = new List<int>();
                foreach (BizFeeCore _collec in _lst) { lstBillDetailid.Add(_collec.feeid); }
                switch (_strOptlb)
                {
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        List<BIZ_FEE_DETAIL_fin> lstIns = fcctx.BIZ_FEE_DETAIL_fin.Where(n => n.billid == 0
                                                                                                                                    && n.cusid == entityBill.cusid
                                                                                                                                    && n.rplb == entityBill.rplb
                                                                                                                                    && n.pid == "0"
                                                                                                                                    && lstBillDetailid.Contains(n.feeid)).ToList();
                        foreach (BIZ_FEE_DETAIL_fin _entity in lstIns)
                        {
                            if(_entity.amt_rectotal==0&& _entity.amt_paytotal == 0 ) throw new BizException("加入账单的业务费用【编号" + _entity.feeid.ToString() + "】不能为0");
                            if (_entity.comcd != entityBill.comcd) throw new BizException("业务费用【编号" + _entity.feeid.ToString() + "】与账单归属不同");
                            if (_entity.invotp != entityBill.invotp) throw new BizException("业务费用【编号" + _entity.feeid.ToString() + "】与账单的发票类型不同");
                            _entity.billid = _ibillid;
                            _entity.pid = "1";
                            if (entityBill.rplb == "R") entityBill.amt_rec += _entity.amt_rectotal;
                            else entityBill.amt_pay += _entity.amt_paytotal;
                        }
                        break;
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        List<BIZ_FEE_DETAIL_fin> lstDel = fcctx.BIZ_FEE_DETAIL_fin.Where(n => n.billid == _ibillid && n.pid == "1").ToList();
                        foreach (BIZ_FEE_DETAIL_fin _entity in lstDel)
                        {
                            if (!lstBillDetailid.Contains(_entity.feeid)) continue;
                            _entity.billid = 0;
                            _entity.pid = "0";
                            if (entityBill.rplb == "R") entityBill.amt_rec -= _entity.amt_rectotal;
                            else entityBill.amt_pay -= _entity.amt_paytotal;
                        }
                        break;
                    default: throw new Exception("操作标识符异常");
                }
                fcctx.SubmitChanges();
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespBillRevFlow GetBillRevFlow(int _iApplyid,string _strApplytp)
        {
            RespBillRevFlow respReturn = new RespBillRevFlow();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<ApplyRevFlow> lst = fcctx.VIEW_FIN_REVFLOWINFO.Where(n => n.applyid == _iApplyid && n.applytp.ToUpper() == _strApplytp)
                                                                                                    .OrderBy(n => n.sortval).AsEnumerable()
                                                                                                    .Select(n => new ApplyRevFlow
                                                                                                    {
                                                                                                        revdt1 = n.revdt1 == null ? "" : ((DateTime)n.revdt1).ToString("yyyy-MM-dd HH:mm"),
                                                                                                        revdt2 = n.revdt2 == null ? "" : ((DateTime)n.revdt2).ToString("yyyy-MM-dd HH:mm"),
                                                                                                        reviewernm1 = n.reviewernm1,
                                                                                                        reviewernm2 = n.reviewernm2,
                                                                                                        revlb1 = n.revlb1,
                                                                                                        revlb2 = n.revlb2,
                                                                                                        revtpnm = n.revtp == "0" ? "选审" : "共审",
                                                                                                        sortval = n.sortval,
                                                                                                        submitdt = n.submitdt == null ? "" : ((DateTime)n.submitdt).ToString("yyyy-MM-dd HH:mm")
                                                                                                    }).ToList();
                respReturn.result = new ResultBillRevFlow { applyrevflow = lst };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespProjDtlFee GetProjDtlFee(int _iBillid)
        {
            RespProjDtlFee respReturn = new RespProjDtlFee();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<int> lstProjdtlid = fcctx.BIZ_FEE_DETAIL_fin.Where(n => n.billid == _iBillid).Select(n => n.projdtlid).Distinct().ToList();
                List<ProjDtlInfoExt> lst = fcctx.VIEW_BIZ_PROJDTLINFO_fin.Where(n => lstProjdtlid.Contains(n.projdtlid)).AsEnumerable()
                                                                                                                .Select(n => new ProjDtlInfoExt
                                                                                                                {
                                                                                                                    comcd = n.comcd,
                                                                                                                    createdt = n.createdt.ToString("yyyy-MM-dd"),
                                                                                                                    createid = n.createid,
                                                                                                                    dtlnm = n.dtlnm,
                                                                                                                    dtltp = n.dtltp,
                                                                                                                    model = n.model,
                                                                                                                    spec = n.spec,
                                                                                                                    optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                                                    projdtlid = n.projdtlid,
                                                                                                                    projid = n.projid,
                                                                                                                    remark = n.remark,
                                                                                                                    unit = n.unit,
                                                                                                                    amt_recprice = n.amt_recprice,
                                                                                                                    amt_rectotal = n.amt_rectotal,
                                                                                                                    amt_refprice = n.amt_refprice,
                                                                                                                    amt_reftotal = n.amt_reftotal,
                                                                                                                    amt_paytotal = n.amt_paytotal,
                                                                                                                    amt_approved = n.amt_approved,
                                                                                                                    selnum = n.recnum,
                                                                                                                    taxrate = n.taxrate,
                                                                                                                    amt_selprice = n.amt_selprice,
                                                                                                                    amt_seltotal = n.amt_seltotal,
                                                                                                                    dtlnum = n.dtlnum,
                                                                                                                    dtltpnm = n.dtltpnm,
                                                                                                                    projnm = n.projnm,
                                                                                                                    feepaycount = n.feepaycount,
                                                                                                                    ctid = n.ctid,
                                                                                                                    ctnm = n.ctnm,
                                                                                                                    ctno = n.ctno,
                                                                                                                    ctsubid = n.ctsubid,
                                                                                                                    feereccount = n.feereccount
                                                                                                                }).ToList();
                respReturn.result = new ResultProjDtl { projdtlinfoextlist = lst };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespReviewCase GetRevCase(string _strComcd,string _strDeptcd,int _iCaseid,string _strValid,string _strCasetp)
        {
            RespReviewCase respReturn = new RespReviewCase();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<ReviewCaseExt> lst = fcctx.VIEW_FIN_REVCASEINFO.Where(n => (string.IsNullOrEmpty(_strComcd) ? true : n.comcd == _strComcd)
                                                                                                                     && (string.IsNullOrEmpty(_strDeptcd) ? true : n.deptcd == _strDeptcd)
                                                                                                                     && (string.IsNullOrEmpty(_strCasetp) ? true : n.casetp == _strCasetp)
                                                                                                                     && (string.IsNullOrEmpty(_strValid) ? true : n.validlb == _strValid)
                                                                                                                     && (_iCaseid == 0 ? true : n.revcaseid == _iCaseid)).AsEnumerable()
                                                                                                        .Select(n => new ReviewCaseExt
                                                                                                        {
                                                                                                            casenm = n.casenm,
                                                                                                            casetp = n.casetp,
                                                                                                            casetpnm = n.casetpnm,
                                                                                                            comcd = _strComcd,
                                                                                                            createdt = DateTime.Now.ToString("yyyy-MM-dd HH:mm"),
                                                                                                            creator = n.username,
                                                                                                            deptcd = n.deptcd,
                                                                                                            optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                                            revcaseid = n.revcaseid,
                                                                                                            sortval = n.sortval,
                                                                                                            validlb = n.validlb
                                                                                                        }).ToList();
                respReturn.result = new ResultReviewCase { revcaselist = lst };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFinaNullResultInfo UpdateRevCase(string _strMemberid, ReviewCase _collec)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                switch (_collec.optlb)
                {
                    #region 新增
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        int iCaseid = sysLogic.GetRefNo("REVC");
                        if (iCaseid == 0) throw new BizException("获取流水号失败");
                        FIN_REVCASETB entityIns = new FIN_REVCASETB
                        {
                            casenm = _collec.casenm ?? "",
                            casetp = _collec.casetp??"",
                            comcd = _collec.comcd,
                            createdt = DateTime.Now,
                            createid = _strMemberid,
                            deptcd = _collec.deptcd,
                            revcaseid = iCaseid,
                            sortval = _collec.sortval,
                            validlb = string.IsNullOrEmpty(_collec.validlb) ? "Y" : _collec.validlb
                        };
                        fcctx.FIN_REVCASETB.InsertOnSubmit(entityIns);
                        break;
                    #endregion
                    #region 修改
                    case GlobalSettings.ENTITYOPERATION_UPDATE:
                        FIN_REVCASETB entityUpd = fcctx.FIN_REVCASETB.SingleOrDefault(n => n.revcaseid == _collec.revcaseid);
                        if (entityUpd == default(FIN_REVCASETB)) throw new BizException("未找到审批方案信息");
                        entityUpd.casenm = string.IsNullOrEmpty(_collec.casenm) ? entityUpd.casenm : _collec.casenm;
                        entityUpd.casetp = string.IsNullOrEmpty(_collec.casetp) ? entityUpd.casetp : _collec.casetp;
                        entityUpd.sortval = _collec.sortval;
                        entityUpd.validlb = string.IsNullOrEmpty(_collec.validlb) ? entityUpd.validlb : _collec.validlb;
                        break;
                    #endregion
                    #region 删除
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        if (fcctx.FIN_REVCASE_DETAIL.Count(n => n.revcaseid == _collec.revcaseid) > 0)
                            throw new BizException("该方案有审批明细，无法删除");
                        FIN_REVCASETB entityDel = fcctx.FIN_REVCASETB.SingleOrDefault(n => n.revcaseid == _collec.revcaseid);
                        if (entityDel == default(FIN_REVCASETB)) throw new BizException("未找到审批方案信息");
                        fcctx.FIN_REVCASETB.DeleteOnSubmit(entityDel);
                        break;
                    #endregion
                    default: throw new Exception("操作标识符异常");
                }
                fcctx.SubmitChanges();
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }


        public static RespReviewCaseDetail GetRevCaseDetail(int _iCaseid)
        {
            RespReviewCaseDetail respReturn = new RespReviewCaseDetail();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<ReviewCaseDetailExt> lst = fcctx.VIEW_FIN_REVCASEDETAIL.Where(n => (n.revcaseid == _iCaseid)).AsEnumerable()
                                                                                                                .Select(n => new ReviewCaseDetailExt
                                                                                                                {
                                                                                                                    optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                                                    revcaseid = n.revcaseid,
                                                                                                                    reviewer1 = n.reviewer1,
                                                                                                                    reviewer2 = n.reviewer2,
                                                                                                                    reviewernm1 = n.reviewernm1,
                                                                                                                    reviewernm2 = n.reviewernm2,
                                                                                                                    revtp = n.revtp,
                                                                                                                    revtpnm = n.revtp == "0" ? "选审" : "共审",
                                                                                                                    sortval = n.sortval
                                                                                                                }).ToList();
                respReturn.result = new ResultReviewCaseDetail { revcasedetaillist = lst };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFinaNullResultInfo UpdateRevCaseDetail(ReviewCaseDetail _collec)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                switch (_collec.optlb)
                {
                    #region 新增
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        if (_collec.reviewer1 == _collec.reviewer2) throw new BizException("首选审批人与次要审批人不能相同");
                        int iCaseDtlid = sysLogic.GetRefNo("REVCD");
                        if (iCaseDtlid == 0) throw new BizException("获取流水号失败");
                        FIN_REVCASE_DETAIL entityIns = new FIN_REVCASE_DETAIL
                        {
                            revcaseid = _collec.revcaseid,
                            reviewer1 = _collec.reviewer1,
                            reviewer2 = _collec.reviewer2 ?? ""
                        };
                        if (string.IsNullOrEmpty(_collec.reviewer2) && _collec.revtp == "1") throw new BizException("独立审批时不能共审");
                        if (_collec.reviewer1 == _collec.reviewer2 && _collec.revtp == "0") throw new BizException("选审时两位审批人不能相同");
                        entityIns.revtp = _collec.revtp;
                        entityIns.sortval = _collec.sortval;
                        fcctx.FIN_REVCASE_DETAIL.InsertOnSubmit(entityIns);
                        break;
                    #endregion
                    #region 修改
                    case GlobalSettings.ENTITYOPERATION_UPDATE:
                        FIN_REVCASE_DETAIL entityUpd = fcctx.FIN_REVCASE_DETAIL.SingleOrDefault(n => n.revcaseid == _collec.revcaseid && n.sortval == _collec.sortval);
                        if (entityUpd == default(FIN_REVCASE_DETAIL)) throw new BizException("未找到审批方案明细");
                        entityUpd.reviewer1 = string.IsNullOrEmpty(_collec.reviewer1) ? entityUpd.reviewer1 : _collec.reviewer1;
                        entityUpd.reviewer2 = _collec.reviewer2 ?? entityUpd.reviewer2;
                        if(entityUpd.reviewer2!=""&& _collec.revtp == "1") throw new BizException("独立审批时不能共审");
                        if (entityUpd.reviewer1 == entityUpd.reviewer2) throw new BizException("首选审批人与次要审批人不能相同");
                        entityUpd.revtp = _collec.revtp;
                        break;
                    #endregion
                    #region 删除
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        FIN_REVCASE_DETAIL entityDel = fcctx.FIN_REVCASE_DETAIL.SingleOrDefault(n => n.revcaseid == _collec.revcaseid && n.sortval == _collec.sortval);
                        if (entityDel == default(FIN_REVCASE_DETAIL)) throw new BizException("未找到审批方案明细");
                        fcctx.FIN_REVCASE_DETAIL.DeleteOnSubmit(entityDel);
                        break;
                    #endregion
                    default: throw new Exception("操作标识符异常");
                }
                fcctx.SubmitChanges();
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }


        public static RespBill GetApprovalBillExtInfo(string _strReviewer, string _strCreateid, string _strComcd, int _iPageSize, int _iCurrPage, int _iBillid, int _iCusid, string _strRplb, string _strRevlb)
        {
            RespBill respReturn = new RespBill();
            bool hasNextPage = false;
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if (_iPageSize == 0) throw new BizException("每页显示条数不能为0");
                int iRecTotal, iPageCount;
                iRecTotal = fcctx.VIEW_FIN_APPROVAL_BILL.Count(n => (n.revlb1 == _strRevlb || n.revlb2 == _strRevlb)
                                                                                            && (n.reviewer1 == _strReviewer || n.reviewer2 == _strReviewer)
                                                                                            && (n.applytp == GlobalSettings.APPLYTP_BILL)
                                                                                            && (string.IsNullOrEmpty(_strComcd) ? true : n.comcd == _strComcd)
                                                                                            && (string.IsNullOrEmpty(_strCreateid) ? true : n.createid == _strCreateid)
                                                                                            && (string.IsNullOrEmpty(_strRplb) ? true : n.rplb == _strRplb)
                                                                                            && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                            && (_iBillid == 0 ? true : n.billid == _iBillid));
                if ((iRecTotal / _iPageSize - _iCurrPage) > 0) hasNextPage = true;
                else if ((iRecTotal / _iPageSize - _iCurrPage) == 0)
                {
                    if ((iRecTotal % _iPageSize) > 0) hasNextPage = true;
                }
                iPageCount = (iRecTotal == 0 ? 0 : iRecTotal / _iPageSize + 1);


       
                List<BillExtInfo> lstBillExt = fcctx.VIEW_FIN_APPROVAL_BILL.Where(n => (n.revlb1 == _strRevlb || n.revlb2 == _strRevlb)
                                                                                            && (n.reviewer1 == _strReviewer || n.reviewer2 == _strReviewer)
                                                                                            && (n.applytp == GlobalSettings.APPLYTP_BILL)
                                                                                            && (string.IsNullOrEmpty(_strComcd) ? true : n.comcd == _strComcd)
                                                                                            && (string.IsNullOrEmpty(_strCreateid) ? true : n.createid == _strCreateid)
                                                                                            && (string.IsNullOrEmpty(_strRplb) ? true : n.rplb == _strRplb)
                                                                                            && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                            && (_iBillid == 0 ? true : n.billid == _iBillid))
                                                                                            .OrderBy(n => n.billid)
                                                                                            .Take(_iPageSize * _iCurrPage).Skip(_iPageSize * (_iCurrPage - 1)).AsEnumerable()
                                                                                            .Select(n => new BillExtInfo
                                                                                            {
                                                                                                amt_pay = n.amt_pay,
                                                                                                amt_rec = n.amt_rec,
                                                                                                amt_pre = n.amt_pre,
                                                                                                amt_pay_ed = n.amt_pay_ed,
                                                                                                amt_rec_ed = n.amt_rec_ed,
                                                                                                approvalsort = n.sortval,
                                                                                                billid = n.billid,
                                                                                                billtp = n.billtp,
                                                                                                billtpnm = n.billtpnm,
                                                                                                createdt = n.createdt.ToString("yyyy-MM-dd HH:mm"),
                                                                                                creator = n.creator,
                                                                                                cusid = n.cusid,
                                                                                                cusnm = n.cusnm,
                                                                                                invotp = n.invotp,
                                                                                                optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                                pid = n.pid,
                                                                                                pidnm = n.pidnm,
                                                                                                remark = n.remark,
                                                                                                revcaseid = n.revcaseid,
                                                                                                revcasenm = n.revcasenm,
                                                                                                rplb = n.rplb,
                                                                                                submitdt = (n.submitdt == null ? "" : ((DateTime)n.submitdt).ToString("yyyy-MM-dd HH:mm")),
                                                                                                taxrate = n.taxrate
                                                                                            }).ToList();
                respReturn.result = new ResultBill { billextlist = lstBillExt, hasnextpage = hasNextPage, pagecount = iPageCount, reccount = iRecTotal };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFeeNotice GetApprovalFnExtInfo(string _strReviewer,string _strCreateid,string _strComcd,int _iPageSize,int _iCurrPage,int _iFnid,int _iCusid,string _strRevlb)
        {
            RespFeeNotice respReturn = new RespFeeNotice();
            bool hasNextPage = false;
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if (_iPageSize == 0) throw new BizException("每页显示条数不能为0");
                int iRecTotal, iPageCount;
                iRecTotal = fcctx.VIEW_FIN_APPROVAL_FN.Count(n => (n.revlb1 == _strRevlb || n.revlb2 == _strRevlb)
                                                                                       && (n.reviewer1 == _strReviewer || n.reviewer2 == _strReviewer)
                                                                                       && (n.applytp == GlobalSettings.APPLYTP_FN)
                                                                                       && (string.IsNullOrEmpty(_strComcd) ? true : n.comcd == _strComcd)
                                                                                       && (string.IsNullOrEmpty(_strCreateid) ? true : n.createid == _strCreateid)
                                                                                       && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                       && (_iFnid == 0 ? true : n.fnid == _iFnid));
                if ((iRecTotal / _iPageSize - _iCurrPage) > 0) hasNextPage = true;
                else if ((iRecTotal / _iPageSize - _iCurrPage) == 0)
                {
                    if ((iRecTotal % _iPageSize) > 0) hasNextPage = true;
                }
                iPageCount = (iRecTotal == 0 ? 0 : iRecTotal / _iPageSize + 1);
                List<FnExtInfo> lstFnExtInfo = fcctx.VIEW_FIN_APPROVAL_FN.Where(n => (n.revlb1 == _strRevlb || n.revlb2 == _strRevlb)
                                                                                        && (n.reviewer1 == _strReviewer || n.reviewer2 == _strReviewer)
                                                                                        && (n.applytp == GlobalSettings.APPLYTP_FN)
                                                                                        && (string.IsNullOrEmpty(_strComcd) ? true : n.comcd == _strComcd)
                                                                                        && (string.IsNullOrEmpty(_strCreateid) ? true : n.createid == _strCreateid)
                                                                                        && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                        && (_iFnid == 0 ? true : n.fnid == _iFnid))
                                                                                        .OrderBy(n => n.fnid)
                                                                                        .Take(_iPageSize * _iCurrPage).Skip(_iPageSize * (_iCurrPage - 1)).AsEnumerable()
                                                                                        .Select(n => new FnExtInfo
                                                                                        {
                                                                                            amt_settle = n.amt_settle,
                                                                                            billid = n.billid,
                                                                                            billtpnm = n.billtpnm,
                                                                                            comcd = n.comcd,
                                                                                            createdt = n.createdt.ToString("yyyy-MM-dd"),
                                                                                            creator = n.creator,
                                                                                            cusid = n.cusid,
                                                                                            cusnm = n.cusnm,
                                                                                            fnid = n.fnid,
                                                                                            fntp = n.fntp,
                                                                                            fntpnm = n.fntpnm,
                                                                                            invotp = n.invotp,
                                                                                            invotpnm = n.invotp,
                                                                                            optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                            pid = n.pid,
                                                                                            pidnm = n.pidnm,
                                                                                            remark = n.remark,
                                                                                            revcaseid = n.revcaseid,
                                                                                            revcasenm = n.revcasenm,
                                                                                            approvalsort = n.sortval,
                                                                                            settledt = "",
                                                                                            rplb = n.rplb,
                                                                                            rplbnm = n.rplbnm,
                                                                                            settletp = n.settletp,
                                                                                            settletpnm = n.settletp,
                                                                                            submitdt = (n.submitdt == null ? "" : ((DateTime)n.submitdt).ToString("yyyy-MM-dd HH:mm")),
                                                                                            settlenm = n.settlenm,
                                                                                            amt_pre = n.amt_pre,
                                                                                            amt_settled_bill = (n.rplb == GlobalSettings.FEELB_RECEIVE ? n.amt_rec_ed : n.amt_pay_ed),
                                                                                            amt_total = (n.rplb == GlobalSettings.FEELB_RECEIVE ? n.amt_rec : n.amt_pay),
                                                                                            taxrate = n.taxrate
                                                                                        }).ToList();
                respReturn.result = new ResultFn { fnextlist = lstFnExtInfo, hasnextpage = hasNextPage, pagecount = iPageCount, reccount = iRecTotal };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespApprovalCount GetApprovalCount(string _strMemberid, string _strApplytp)
        {
            RespApprovalCount respReturn = new RespApprovalCount();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                int iApprovalCount = 0;
                switch(_strApplytp.ToUpper())
                {
                    case GlobalSettings.APPLYTP_BILL:
                        iApprovalCount = fcctx.VIEW_FIN_APPROVAL_BILL.Count(n => (n.reviewer1 == _strMemberid && n.revlb1 == GlobalSettings.NOLB) || (n.reviewer2 == _strMemberid && n.revlb2 == GlobalSettings.NOLB));
                        break;
                    case GlobalSettings.APPLYTP_FN:
                        break;
                    default:throw new BizException("申请单类别异常");
                }
                respReturn.result = new ResultApprovalCount { approvalcount = iApprovalCount };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespReviewDeny GetApprovalDenyInfo(int _iApplyid,string _strApplytp)
        {
            RespReviewDeny respReturn = new RespReviewDeny();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<ReviewDenyExtInfo> lst = fcctx.VIEW_FIN_REVDENYINFO.Where(n => n.applyid == _iApplyid && n.applytp == _strApplytp).OrderBy(n => n.advid).AsEnumerable()
                                                                                                            .Select(n => new ReviewDenyExtInfo
                                                                                                            {
                                                                                                                advice = n.advice,
                                                                                                                applyid = n.applyid,
                                                                                                                applytp = n.applytp,
                                                                                                                createdt = n.createdt.ToString("yyyy-MM-dd HH:mm"),
                                                                                                                reviewer = n.reviewer
                                                                                                            }).ToList();
                respReturn.result = new ReslutReviewDeny { denylist = lst };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFinaNullResultInfo BillApprove(string _strMemberid, int _iBillid,string _strRevlb,int _iRevSort, string _strReason,int _iPersonid=0)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            string strMemberid = _strMemberid;
            try
            {
                FIN_BILLTB entityBill = fcctx.FIN_BILLTB.SingleOrDefault(n => n.billid == _iBillid);
                if (entityBill == default(FIN_BILLTB)) throw new BizException("未找到账单信息");
                //微信等外部平台请求只能定位到personid，因此需要先定位其memberid
                if (string.IsNullOrEmpty(strMemberid) && _iPersonid > 0)
                {
                    VIEW_FIN_REVFLOWINFO entityRevFlow = fcctx.VIEW_FIN_REVFLOWINFO.SingleOrDefault(n => n.applyid == _iBillid
                                                                                                                                                                    && n.applytp == GlobalSettings.APPLYTP_BILL
                                                                                                                                                                    && n.sortval == _iRevSort
                                                                                                                                                                    && (n.reviewer1 == _iPersonid.ToString() || n.reviewer2 == _iPersonid.ToString()));
                    if(entityRevFlow==default(VIEW_FIN_REVFLOWINFO)) throw new BizException("未找到审批信息");
                    strMemberid = (entityRevFlow.reviewer1 == _iPersonid.ToString() ? entityRevFlow.reviewer1 : entityRevFlow.reviewer2);
                }
                
                List<FIN_REVFLOW> lstRevFlow = fcctx.FIN_REVFLOW.Where(n => n.applyid == _iBillid && n.applytp == GlobalSettings.APPLYTP_BILL).OrderBy(n => n.sortval).ToList();
                if (lstRevFlow.Count == 0) throw new BizException("未找到审批流程信息");
                FIN_REVFLOW entityApproval = lstRevFlow.SingleOrDefault(n => n.sortval == _iRevSort && (n.reviewer1 == strMemberid || n.reviewer2 == strMemberid));
                if (entityApproval == default(FIN_REVFLOW)) throw new BizException("审批信息出错");
                if (entityApproval.revlb1 != "N" && entityApproval.revlb2 != "N") throw new BizException("审批流程出错");
                DateTime dtNow = DateTime.Now;
                switch (_strRevlb.ToUpper())
                {
                    #region 同意
                    case GlobalSettings.YESLB:
                        int iSelfIndex = lstRevFlow.IndexOf(entityApproval);
                        if (lstRevFlow[iSelfIndex].reviewer1 == strMemberid)
                        {
                            lstRevFlow[iSelfIndex].revlb1 = GlobalSettings.YESLB;
                            lstRevFlow[iSelfIndex].revdt1 = dtNow;
                        }
                        else
                        {
                            lstRevFlow[iSelfIndex].revlb2 = GlobalSettings.YESLB;
                            lstRevFlow[iSelfIndex].revdt2 = dtNow;
                        }
                        if (lstRevFlow[iSelfIndex].revtp == "0") //选审
                        {
                            if (iSelfIndex + 1 == lstRevFlow.Count) entityBill.pid = "3"; //审批完成
                            else
                            {
                                lstRevFlow[iSelfIndex + 1].revlb1 = "N";
                                lstRevFlow[iSelfIndex + 1].revlb2 = string.IsNullOrEmpty(lstRevFlow[iSelfIndex + 1].reviewer2) ? "" : "N";
                                lstRevFlow[iSelfIndex + 1].submitdt = dtNow;
                            }
                        }
                        else //共审
                        {
                            if(lstRevFlow[iSelfIndex].revlb1=="Y"&& lstRevFlow[iSelfIndex].revlb2 == "Y")
                            {
                                if (iSelfIndex + 1 == lstRevFlow.Count) entityBill.pid = "3"; //审批完成
                                else
                                {
                                    lstRevFlow[iSelfIndex + 1].revlb1 = "N";
                                    lstRevFlow[iSelfIndex + 1].revlb2 = string.IsNullOrEmpty(lstRevFlow[iSelfIndex + 1].reviewer2) ? "" : "N";
                                    lstRevFlow[iSelfIndex + 1].submitdt = dtNow;
                                }
                            }
                        }
                        fcctx.SubmitChanges();
                        if(entityBill.pid == "3") //审批流程已完全结束
                        {
                            #region 推送微信消息-审批结果通知-同意
                            extPubWxLogic.SendWxMsgApplyResult(entityBill.createid,
                                                                                        1,
                                                                                        new ExtWxSendMsgData
                                                                                        {
                                                                                            first = new ExtWxSendMsgDataCore { value = "您的账单申请已审批通过，申请单号：#" + _iBillid.ToString() },
                                                                                            keyword1 = new ExtWxSendMsgDataCore { value = _strMemberid }, //审批人
                                                                                        keyword2 = new ExtWxSendMsgDataCore { value = "同意" } //审批内容
                                                                                        });
                            #endregion
                        }
                        break;
                    #endregion
                    #region 拒绝
                    case GlobalSettings.DENYLB:
                        entityBill.pid = "1";
                        List<BIZ_FEE_DETAIL_fin> lstBillDetail = fcctx.BIZ_FEE_DETAIL_fin.Where(n => n.billid == _iBillid).ToList();
                        foreach (BIZ_FEE_DETAIL_fin _entity in lstBillDetail) _entity.pid = "1";

                        FIN_REVDENYTB entityDeny = new FIN_REVDENYTB
                        {
                            advice = _strReason + (_iPersonid > 0 ? "@" + _iPersonid : ""),
                            applyid = _iBillid,
                            applytp = GlobalSettings.APPLYTP_BILL,
                            createdt = DateTime.Now,
                            reviewer = strMemberid
                        };
                        fcctx.FIN_REVDENYTB.InsertOnSubmit(entityDeny);

                        fcctx.FIN_REVFLOW.DeleteAllOnSubmit(lstRevFlow);
                        fcctx.SubmitChanges();
                        #region 推送微信消息-审批结果通知-拒绝
                        extPubWxLogic.SendWxMsgApplyResult(entityBill.createid,
                                                                                    1,
                                                                                    new ExtWxSendMsgData
                                                                                    {
                                                                                        first = new ExtWxSendMsgDataCore { value = "您的账单申请审批未通过，申请单号：#" + _iBillid.ToString() },
                                                                                        keyword1 = new ExtWxSendMsgDataCore { value = _strMemberid }, //审批人
                                                                                        keyword2 = new ExtWxSendMsgDataCore { value = "拒绝", color = "#e62326" }, //审批内容
                                                                                        remark = new ExtWxSendMsgDataCore { value = _strReason }
                                                                                    });
                        #endregion
                        break;
                    #endregion
                    default: throw new Exception("审批标志异常");
                }
         

            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFinaNullResultInfo FnApprove(string _strMemberid, int _iFnid, string _strRevlb, int _iRevSort, string _strReason,int _iPersonid=0)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            string strMemberid = _strMemberid;
            try
            {
                FIN_FEENOTICE entityFn = fcctx.FIN_FEENOTICE.SingleOrDefault(n => n.fnid == _iFnid);
                if (entityFn == default(FIN_FEENOTICE)) throw new BizException("未找到结算单信息");
                //微信等外部平台请求只能定位到personid，因此需要先定位其memberid
                if (string.IsNullOrEmpty(strMemberid) && _iPersonid > 0)
                {
                    VIEW_FIN_REVFLOWINFO entityRevFlow = fcctx.VIEW_FIN_REVFLOWINFO.SingleOrDefault(n => n.applyid == _iFnid
                                                                                                                                                                    && n.applytp == GlobalSettings.APPLYTP_FN
                                                                                                                                                                    && n.sortval == _iRevSort
                                                                                                                                                                    && (n.reviewer1 == _iPersonid.ToString() || n.reviewer2 == _iPersonid.ToString()));
                    if (entityRevFlow == default(VIEW_FIN_REVFLOWINFO)) throw new BizException("未找到审批信息");
                    strMemberid = (entityRevFlow.reviewer1 == _iPersonid.ToString() ? entityRevFlow.reviewer1 : entityRevFlow.reviewer2);
                }
                List<FIN_REVFLOW> lstRevFlow = fcctx.FIN_REVFLOW.Where(n => n.applyid == _iFnid && n.applytp == GlobalSettings.APPLYTP_FN).OrderBy(n => n.sortval).ToList();
                if (lstRevFlow.Count == 0) throw new BizException("未找到审批流程信息");
                FIN_REVFLOW entityApproval = lstRevFlow.SingleOrDefault(n => n.sortval == _iRevSort && (n.reviewer1 == strMemberid || n.reviewer2 == strMemberid));
                if (entityApproval == default(FIN_REVFLOW)) throw new BizException("审批信息出错");
                if (entityApproval.revlb1 != "N" && entityApproval.revlb2 != "N") throw new BizException("审批流程出错");
                DateTime dtNow = DateTime.Now;
                switch (_strRevlb.ToUpper())
                {
                    #region 同意
                    case GlobalSettings.YESLB:
                        int iSelfIndex = lstRevFlow.IndexOf(entityApproval);
                        if (lstRevFlow[iSelfIndex].reviewer1 == strMemberid)
                        {
                            lstRevFlow[iSelfIndex].revlb1 = GlobalSettings.YESLB;
                            lstRevFlow[iSelfIndex].revdt1 = dtNow;
                        }
                        else
                        {
                            lstRevFlow[iSelfIndex].revlb2 = GlobalSettings.YESLB;
                            lstRevFlow[iSelfIndex].revdt2 = dtNow;
                        }
                        if (lstRevFlow[iSelfIndex].revtp == "0") //选审
                        {
                            if (iSelfIndex + 1 == lstRevFlow.Count) entityFn.pid = "3"; //审批完成，结算中
                            else
                            {
                                lstRevFlow[iSelfIndex + 1].revlb1 = "N";
                                lstRevFlow[iSelfIndex + 1].revlb2 = string.IsNullOrEmpty(lstRevFlow[iSelfIndex + 1].reviewer2) ? "" : "N";
                                lstRevFlow[iSelfIndex + 1].submitdt = dtNow;
                            }
                        }
                        else //共审
                        {
                            if (lstRevFlow[iSelfIndex].revlb1 == "Y" && lstRevFlow[iSelfIndex].revlb2 == "Y")
                            {
                                if (iSelfIndex + 1 == lstRevFlow.Count) entityFn.pid = "3"; //审批完成，结算中
                                else
                                {
                                    lstRevFlow[iSelfIndex + 1].revlb1 = "N";
                                    lstRevFlow[iSelfIndex + 1].revlb2 = string.IsNullOrEmpty(lstRevFlow[iSelfIndex + 1].reviewer2) ? "" : "N";
                                    lstRevFlow[iSelfIndex + 1].submitdt = dtNow;
                                }
                            }
                        }
                        //如果当前结算单是审批完成的状态，则需对其所属账单的pid做出相应的调整
                        if(entityFn.pid == "3")
                        {
                            FIN_BILLTB entityBill = fcctx.FIN_BILLTB.SingleOrDefault(n => n.billid == entityFn.billid);
                            if (entityBill == default(FIN_BILLTB)) throw new BizException("未找到账单信息；" + entityFn.billid);
                            if ((entityBill.amt_pay + entityBill.amt_rec) == entityFn.amt_settle) entityBill.pid = "5"; //结算单已审批
                            else entityBill.pid = "4"; //结算单审批中
                        }
                        fcctx.SubmitChanges();
                        if (entityFn.pid == "3") //审批流程已完全结束
                        {
                            #region 推送微信消息-审批结果通知-同意
                            extPubWxLogic.SendWxMsgApplyResult(entityFn.createid,
                                                                                        1,
                                                                                        new ExtWxSendMsgData
                                                                                        {
                                                                                            first = new ExtWxSendMsgDataCore { value = "您的结算单申请已审批通过，申请单号：#" + _iFnid.ToString() },
                                                                                            keyword1 = new ExtWxSendMsgDataCore { value = _strMemberid }, //审批人
                                                                                            keyword2 = new ExtWxSendMsgDataCore { value = "同意" } //审批内容
                                                                                        });
                            #endregion
                        }
                        break;
                    #endregion
                    #region 拒绝
                    case GlobalSettings.DENYLB:
                        entityFn.pid = "0";
                        FIN_BILLTB entityBillDeny = fcctx.FIN_BILLTB.SingleOrDefault(n => n.billid == entityFn.billid);
                        if (entityBillDeny == default(FIN_BILLTB)) throw new BizException("未找到账单信息；" + entityFn.billid);
                        if ((entityBillDeny.amt_pay + entityBillDeny.amt_rec) == entityFn.amt_settle) entityBillDeny.pid = "3"; //业务审批完成
                        else entityBillDeny.pid = "4"; //结算单审批中

                        FIN_REVDENYTB entityDeny = new FIN_REVDENYTB
                        {
                            advice = _strReason + (_iPersonid > 0 ? "@" + _iPersonid : ""),
                            applyid = _iFnid,
                            applytp = GlobalSettings.APPLYTP_FN,
                            createdt = DateTime.Now,
                            reviewer = strMemberid
                        };
                        fcctx.FIN_REVDENYTB.InsertOnSubmit(entityDeny);

                        fcctx.FIN_REVFLOW.DeleteAllOnSubmit(lstRevFlow);
                        fcctx.SubmitChanges();
                        #region 推送微信消息-审批结果通知-拒绝
                        extPubWxLogic.SendWxMsgApplyResult(entityFn.createid,
                                                                                    1,
                                                                                    new ExtWxSendMsgData
                                                                                    {
                                                                                        first = new ExtWxSendMsgDataCore { value = "您的结算单申请审批未通过，申请单号：#" + _iFnid.ToString() },
                                                                                        keyword1 = new ExtWxSendMsgDataCore { value = _strMemberid }, //审批人
                                                                                        keyword2 = new ExtWxSendMsgDataCore { value = "拒绝", color = "#e62326" }, //审批内容
                                                                                        remark = new ExtWxSendMsgDataCore { value = _strReason }
                                                                                    });
                        #endregion
                        break;
                    #endregion
                    default: throw new Exception("审批标志异常");
                }
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFinaNullResultInfo FnApproveBack(string _strMemberid,int _iFnid,string _strReason)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                FIN_FEENOTICE entityFn = fcctx.FIN_FEENOTICE.SingleOrDefault(n => n.fnid == _iFnid);
                if (entityFn == default(FIN_FEENOTICE)) throw new BizException("未找到结算单信息");
                if(entityFn.pid=="9") throw new BizException("该结算单已结算完成，无法操作");
                if (fcctx.FIN_INVOFNMAPPING.Count(n => n.fnid == _iFnid) > 0) throw new BizException("该结算单已包含发票信息，请先删除相关发票信息");
                if (fcctx.FIN_FEENOTICE_DETAIL.Count(n => n.fnid == _iFnid) > 0) throw new BizException("该结算单已包含实收实付信息，请先删除相关收付信息");
                FIN_BILLTB entityBill = fcctx.FIN_BILLTB.SingleOrDefault(n => n.billid == entityFn.billid);
                if (entityBill == default(FIN_BILLTB)) throw new BizException("未找到结算单对应的账单信息");
                if ((entityBill.amt_pay + entityBill.amt_rec) == entityFn.amt_settle) entityBill.pid = "3"; //业务审批完成
                else entityBill.pid = "4"; //结算单审批中
                //如结算单状态为【结费完成(欠发票)】，则需修正账单记录中的已结算金额和预结金额
                if (entityFn.pid == "4")
                {
                    if(entityBill.rplb==GlobalSettings.FEELB_PAY) entityBill.amt_pay_ed -= entityFn.amt_settle;
                    else if (entityBill.rplb == GlobalSettings.FEELB_RECEIVE) entityBill.amt_rec_ed -= entityFn.amt_settle;
                    entityBill.amt_pre += entityFn.amt_settle;
                    if (entityBill.amt_pay_ed < 0 || entityBill.amt_rec_ed < 0) throw new BizException("账单已结算金额有误");
                }
                fcctx.FIN_REVDENYTB.InsertOnSubmit(new FIN_REVDENYTB
                {
                    advice = _strReason+" #SensBack;FromFnPid"+ entityFn.pid+";Billid"+entityBill.billid.ToString(),
                    applyid = _iFnid,
                    applytp = GlobalSettings.APPLYTP_FN,
                    createdt = DateTime.Now,
                    reviewer = _strMemberid
                });
                fcctx.FIN_REVFLOW.DeleteAllOnSubmit(fcctx.FIN_REVFLOW.Where(n => n.applyid == _iFnid && n.applytp == GlobalSettings.APPLYTP_FN));
                entityFn.pid = "0";
                fcctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFinaNullResultInfo BillApproveBack(string _strMemberid,int _iBillid,string _strReason)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                FIN_BILLTB entityBill = fcctx.FIN_BILLTB.SingleOrDefault(n => n.billid == _iBillid);
                if (entityBill == default(FIN_BILLTB)) throw new BizException("未找到账单信息");
                if(entityBill.pid=="9") throw new BizException("该账单已结算完成，无法退回");
                if(entityBill.billtp_inner==GlobalSettings.FNTP_ADM) throw new BizException("该账单不属于业务类账单，无法退回");
                if (fcctx.FIN_FEENOTICE.Count(n => n.billid == _iBillid) > 0) throw new BizException("该账单已包含结算单，请先删除结算单");
                fcctx.FIN_REVDENYTB.InsertOnSubmit(new FIN_REVDENYTB
                {
                    advice = _strReason + " #SensBack FromPid" + entityBill.pid,
                    applyid = _iBillid,
                    applytp = GlobalSettings.APPLYTP_BILL,
                    createdt = DateTime.Now,
                    reviewer = _strMemberid
                });
                fcctx.FIN_REVFLOW.DeleteAllOnSubmit(fcctx.FIN_REVFLOW.Where(n => n.applyid == _iBillid && n.applytp == GlobalSettings.APPLYTP_BILL));
                entityBill.pid = "1";
                foreach (BIZ_FEE_DETAIL_fin _entity in fcctx.BIZ_FEE_DETAIL_fin.Where(n => n.billid == _iBillid)) _entity.pid = "1";
                fcctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFeeNotice GetBizFeeNoticeExtInfo(string _strCreateid, string _strComcd, int _iPageSize, int _iCurrPage,int _iBillid, int _iFnid, int _iCusid, string _strRplb, string[] _strsPid)
        {
            RespFeeNotice respReturn = new RespFeeNotice();
            bool hasNextPage = false;
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if (_iPageSize == 0) throw new BizException("每页显示条数不能为0");
                int iRecTotal, iPageCount;
                iRecTotal = fcctx.VIEW_FIN_FEENOTICEINFO.Count(n => (string.IsNullOrEmpty(_strComcd) ? true : n.comcd == _strComcd)
                                                                                            && (string.IsNullOrEmpty(_strCreateid) ? true : n.createid == _strCreateid)
                                                                                            && (string.IsNullOrEmpty(_strRplb) ? true : n.rplb == _strRplb)
                                                                                            && ((_strsPid.Length == 1 && _strsPid[0] == "") ? true : _strsPid.Contains(n.pid))
                                                                                            && (n.fntp == GlobalSettings.FNTP_BIZ)
                                                                                            && (_iBillid == 0 ? true : n.billid == _iBillid)
                                                                                            && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                            && (_iFnid == 0 ? true : n.fnid == _iFnid));
                if ((iRecTotal / _iPageSize - _iCurrPage) > 0) hasNextPage = true;
                else if ((iRecTotal / _iPageSize - _iCurrPage) == 0)
                {
                    if ((iRecTotal % _iPageSize) > 0) hasNextPage = true;
                }
                iPageCount = (iRecTotal == 0 ? 0 : iRecTotal / _iPageSize + 1);
                List<FnExtInfo> lstFnExt = fcctx.VIEW_FIN_FEENOTICEINFO.Where(n => (string.IsNullOrEmpty(_strComcd) ? true : n.comcd == _strComcd)
                                                                                            && (string.IsNullOrEmpty(_strCreateid) ? true : n.createid == _strCreateid)
                                                                                            && (string.IsNullOrEmpty(_strRplb) ? true : n.rplb == _strRplb)
                                                                                            && ((_strsPid.Length == 1 && _strsPid[0] == "") ? true : _strsPid.Contains(n.pid))
                                                                                            && (n.fntp == GlobalSettings.FNTP_BIZ)
                                                                                            && (_iBillid == 0 ? true : n.billid == _iBillid)
                                                                                            && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                            && (_iFnid == 0 ? true : n.fnid == _iFnid))
                                                                                            .OrderBy(n => n.fnid)
                                                                                            .Take(_iPageSize * _iCurrPage).Skip(_iPageSize * (_iCurrPage - 1)).AsEnumerable()
                                                                                            .Select(n => new FnExtInfo
                                                                                            {
                                                                                                amt_settle = n.amt_settle,
                                                                                                billid = n.billid,
                                                                                                fnid = n.fnid,
                                                                                                comcd = n.comcd,
                                                                                                invotpnm = n.invotpnm,
                                                                                                rplbnm = n.rplbnm,
                                                                                                settletp = n.settletp,
                                                                                                settletpnm = n.settletpnm,
                                                                                                createdt = n.createdt.ToString("yyyy-MM-dd HH:mm"),
                                                                                                creator = n.creator,
                                                                                                cusid = n.cusid,
                                                                                                cusnm = n.cusnm,
                                                                                                invotp = n.invotp,
                                                                                                optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                                pid = n.pid,
                                                                                                pidnm = n.pidnm,
                                                                                                printlb = n.printlb,
                                                                                                remark = n.remark,
                                                                                                revcaseid = n.revcaseid,
                                                                                                revcasenm = n.casenm,
                                                                                                settlenm = n.settlenm,
                                                                                                settledt = (n.settledt == null ? "" : ((DateTime)n.settledt).ToString("yyyy-MM-dd HH:mm")),
                                                                                                amt_pre = n.amt_pre,
                                                                                                amt_settled_bill = (n.rplb == GlobalSettings.FEELB_RECEIVE ? n.amt_rec_ed : n.amt_pay_ed),
                                                                                                amt_settled_fn = n.amt_settled_fn,
                                                                                                amt_total = (n.rplb == GlobalSettings.FEELB_RECEIVE ? n.amt_rec : n.amt_pay),
                                                                                                amt_invo_total = n.amt_invo_total,
                                                                                                rplb = n.rplb,
                                                                                                fntp = n.fntp,
                                                                                                taxrate = n.taxrate
                                                                                            }).ToList();
                respReturn.result = new ResultFn { fnextlist = lstFnExt, hasnextpage = hasNextPage, pagecount = iPageCount, reccount = iRecTotal };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFinaNullResultInfo UpdateFeeNoticeInfo(string _strMemberid,string _strComcd, FnBaseInfo _collec)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                int iFnDetailCount;
                switch (_collec.optlb)
                {
                    #region 新增
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        int iCusid = _collec.cusid;
                        //业务类结算单必需有对应的已审核账单
                        if (_collec.fntp == GlobalSettings.FNTP_BIZ)
                        {
                            FIN_BILLTB entityBill = fcctx.FIN_BILLTB.SingleOrDefault(n => n.billid == _collec.billid && n.comcd == _strComcd);
                            if (entityBill == default(FIN_BILLTB)) throw new BizException("未找到相应的账单信息");
                            if (string.Compare(entityBill.pid, "3") < 0) throw new BizException("账单审批未完成，不能进行结算");
                            if (entityBill.rplb != _collec.rplb) throw new BizException("收付费类型不一致");
                            if (_collec.cusid != 0 && entityBill.cusid != _collec.cusid) throw new BizException("收付费客户不一致");
                            if ((entityBill.billtp == GlobalSettings.FEELB_RECEIVE && 
                                ((entityBill.amt_pre + _collec.amt_settle + entityBill.amt_rec_ed > entityBill.amt_rec &&entityBill.amt_rec>0)||
                                (entityBill.amt_pre + _collec.amt_settle + entityBill.amt_rec_ed < entityBill.amt_rec && entityBill.amt_rec < 0)))
                                || ((entityBill.billtp == GlobalSettings.FEELB_PAY) && 
                                ((entityBill.amt_pre + _collec.amt_settle + entityBill.amt_pay_ed > entityBill.amt_pay&&entityBill.amt_pay>0)||
                                (entityBill.amt_pre + _collec.amt_settle + entityBill.amt_pay_ed < entityBill.amt_pay && entityBill.amt_pay < 0))))
                                throw new BizException("结算金额超限");
                            //如果账单金额为 负数（冲抵），则结算金额也必需为 负数（冲抵），反之亦然
                            if (((entityBill.amt_rec > 0 || entityBill.amt_pay > 0) && _collec.amt_settle < 0) ||
                                ((entityBill.amt_rec < 0 || entityBill.amt_pay < 0) && _collec.amt_settle > 0))
                                throw new BizException("结算金额与结算类别不符");
                            entityBill.amt_pre += _collec.amt_settle;
                            iCusid = entityBill.cusid;
                        }
                        
                        int iFnid = sysLogic.GetRefNo("FN");
                        if (iFnid == 0) throw new BizException("获取流水号失败");
                        FIN_FEENOTICE entityIns = new FIN_FEENOTICE
                        {
                            amt_settle = _collec.amt_settle,
                            comcd = _strComcd,
                            billid = _collec.fntp == GlobalSettings.FNTP_BIZ ? _collec.billid : 0,
                            createdt = DateTime.Now,
                            createid = _strMemberid,
                            cusid = iCusid,
                            fnid = iFnid,
                            fntp = _collec.fntp,
                            invotp = _collec.invotp ?? "",
                            pid = "0",
                            printlb = GlobalSettings.NOLB,
                            remark = _collec.remark ?? "",
                            revcaseid = _collec.revcaseid,
                            rplb = _collec.rplb,
                            taxrate = _collec.taxrate,
                            settletp = _collec.settletp ?? "",
                            settleid = ""
                        };
                        fcctx.FIN_FEENOTICE.InsertOnSubmit(entityIns);
                        break;
                    #endregion
                    #region 修改
                    case GlobalSettings.ENTITYOPERATION_UPDATE:
                        FIN_FEENOTICE entityUpd = fcctx.FIN_FEENOTICE.SingleOrDefault(n => n.fnid == _collec.fnid);
                        if (entityUpd == default(FIN_FEENOTICE)) throw new BizException("未找到结算单信息");
                        if (string.Compare(entityUpd.pid, "1") > 0) throw new BizException("结算单正在审核，无法修改");
                        if (entityUpd.fntp == GlobalSettings.FNTP_ADM)
                        {
                            entityUpd.cusid = _collec.cusid == 0 ? entityUpd.cusid : _collec.cusid;
                            entityUpd.amt_settle = _collec.amt_settle == 0 ? entityUpd.amt_settle : _collec.amt_settle;
                        }
                        entityUpd.invotp = string.IsNullOrEmpty(_collec.invotp) ? entityUpd.invotp : _collec.invotp;
                        entityUpd.taxrate = _collec.taxrate == 0 ? entityUpd.taxrate : _collec.taxrate;
                        entityUpd.settletp = string.IsNullOrEmpty(_collec.settletp) ? entityUpd.settletp : _collec.settletp;
                        entityUpd.revcaseid = entityUpd.rplb == GlobalSettings.FEELB_RECEIVE ? 0 : _collec.revcaseid;
                        entityUpd.printlb = _collec.printlb == GlobalSettings.YESLB ? _collec.printlb : entityUpd.printlb;
                        entityUpd.remark = _collec.remark ?? entityUpd.remark;
                        break;
                    #endregion
                    #region 删除
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        FIN_FEENOTICE entityDel = fcctx.FIN_FEENOTICE.SingleOrDefault(n => n.fnid == _collec.fnid);
                        if (entityDel == default(FIN_FEENOTICE)) throw new BizException("未找到结算单信息");

                        if (entityDel.fntp == GlobalSettings.FNTP_BIZ)
                        {
                            if(_collec.billid==0) throw new BizException("账单编号有误");
                            FIN_BILLTB entityBill = fcctx.FIN_BILLTB.SingleOrDefault(n => n.billid == _collec.billid);
                            if (entityBill == default(FIN_BILLTB)) throw new BizException("未找到相应的账单信息");
                            if (entityBill.pid=="9") throw new BizException("账单已做实收实付，无法删除结算单");
                            if(entityBill.amt_pre<entityDel.amt_settle) throw new BizException("结算金额超限");
                            if (fcctx.FIN_FEENOTICE.Count(n => n.billid == _collec.billid) == 1) entityBill.pid = "3";
                            entityBill.amt_pre -= entityDel.amt_settle; //删除结算单时扣减账单预结金额
                        }
                      
                        if(entityDel.pid=="9") throw new BizException("结算单已结算完成，无法删除");
                        iFnDetailCount = fcctx.FIN_FEENOTICE_DETAIL.Count(n => n.fnid == _collec.fnid);
                        if (iFnDetailCount > 0) throw new BizException("结算单已有实际结算记录，无法删除");
                        fcctx.FIN_FEENOTICE.DeleteOnSubmit(entityDel);
                        break;
                    #endregion
                    default: throw new Exception("操作标识符异常");
                }
                fcctx.SubmitChanges();
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFinaNullResultInfo SubmitFn(string _strMemberid,string _strComcd,FnCore _collec)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if (_collec.optlb != GlobalSettings.ENTITYOPERATION_UPDATE) throw new Exception("操作标识符异常");
                FIN_FEENOTICE entityUpd = fcctx.FIN_FEENOTICE.SingleOrDefault(n => n.fnid == _collec.fnid);
                if (entityUpd == default(FIN_FEENOTICE)) throw new BizException("未找到结算单信息");
                if (entityUpd.cusid == 0) throw new BizException("未选收付费客户");
                if (string.Compare(entityUpd.pid, "2") > 0) throw new BizException("结算单已提交，无法重复提交");
                FIN_BILLTB entityBill = fcctx.FIN_BILLTB.SingleOrDefault(n => n.billid == entityUpd.billid);
                if (entityBill == default(FIN_BILLTB)) throw new BizException("未找到账单信息；" + entityUpd.billid);
                entityBill.pid = "4"; //部分结算中
                switch (entityUpd.rplb)
                {
                    case GlobalSettings.FEELB_RECEIVE:
                        entityUpd.pid = "3"; //收费联系单无需审批，直接进入结算
                        if ((entityBill.amt_pay + entityBill.amt_rec) == entityUpd.amt_settle) entityBill.pid = "5"; //结算中
                        fcctx.SubmitChanges();
                        break;
                    case GlobalSettings.FEELB_PAY:
                        if (entityUpd.revcaseid == 0 && _collec.revcaseid == 0) throw new BizException("未选择审批方案");
                        if (entityUpd.revcaseid == 0 && _collec.revcaseid != 0) entityUpd.revcaseid = _collec.revcaseid;
                        FIN_REVCASETB entityRevCase = fcctx.FIN_REVCASETB.SingleOrDefault(n => n.revcaseid == entityUpd.revcaseid && n.validlb == "Y");
                        if (entityRevCase == default(FIN_REVCASETB)) throw new BizException("未找到有效的审批方案");
                        List<FIN_REVCASE_DETAIL> lstCaseDtlSour = fcctx.FIN_REVCASE_DETAIL.Where(n => n.revcaseid == entityUpd.revcaseid).ToList();
                        if (lstCaseDtlSour.Count == 0) throw new BizException("未找到可用的审批方案明细");
                        List<FIN_REVFLOW> lstFlow = new List<FIN_REVFLOW>();
                        foreach (FIN_REVCASE_DETAIL _entity in lstCaseDtlSour)
                        {
                            FIN_REVFLOW entityInsFlow = new FIN_REVFLOW
                            {
                                applyid = entityUpd.fnid,
                                applytp = GlobalSettings.APPLYTP_FN,
                                reviewer1 = _entity.reviewer1,
                                reviewer2 = _entity.reviewer2
                            };
                            entityInsFlow.revlb1 = entityInsFlow.revlb2 = "";
                            entityInsFlow.revtp = _entity.revtp;
                            entityInsFlow.sortval = _entity.sortval;
                            lstFlow.Add(entityInsFlow);
                        }
                        lstFlow[0].submitdt = DateTime.Now;
                        lstFlow[0].revlb1 = "N";
                        lstFlow[0].revlb2 = string.IsNullOrEmpty(lstFlow[0].reviewer2) ? "" : "N";
                        fcctx.FIN_REVFLOW.InsertAllOnSubmit(lstFlow);
                        //**********************
                        entityUpd.pid = "2";
                        fcctx.SubmitChanges();
                        //#region 推送微信消息


                        //VIEW_FIN_APPROVAL_ALL entityApply = fcctx.VIEW_FIN_APPROVAL_ALL.FirstOrDefault(n => n.applyid == _collec.billid && n.applytp == GlobalSettings.APPLYTP_FN);
                        //extPubWxLogic.SendWxMsgApplyReq(GlobalSettings.APPLYTP_FN,
                        //                                                            _collec.billid,
                        //                                                            _strComcd,
                        //                                                            lstFlow[0].reviewer1,
                        //                                                            1,
                        //                                                            new ExtWxSendMsgData
                        //                                                            {
                        //                                                                first = new ExtWxSendMsgDataCore { value = "您有一笔申请需要审批，审批单号：#" + _collec.billid },
                        //                                                                keyword1 = new ExtWxSendMsgDataCore { value = entityApply.applytpnm }, //申请类型
                        //                                                        keyword2 = new ExtWxSendMsgDataCore { value = entityApply.creator }, //申请人
                        //                                                        keyword3 = new ExtWxSendMsgDataCore { value = entityApply.submitdt == null ? "" : ((DateTime)entityApply.submitdt).ToString("yyyy-MM-dd HH:mm:ss") },//申请时间
                        //                                                        keyword4 = new ExtWxSendMsgDataCore { value = entityApply.rplbnm + entityApply.amt_apply.ToString() } //申请事由
                        //                                                    });
                        //if (!string.IsNullOrEmpty(lstFlow[0].reviewer2))
                        //{
                        //    extPubWxLogic.SendWxMsgApplyReq(GlobalSettings.APPLYTP_FN,
                        //                                                            _collec.billid,
                        //                                                            _strComcd,
                        //                                                            lstFlow[0].reviewer2,
                        //                                                            1,
                        //                                                            new ExtWxSendMsgData
                        //                                                            {
                        //                                                                first = new ExtWxSendMsgDataCore { value = "您有一笔申请需要审批，审批单号：#" + _collec.billid },
                        //                                                                keyword1 = new ExtWxSendMsgDataCore { value = entityApply.applytpnm },
                        //                                                                keyword2 = new ExtWxSendMsgDataCore { value = entityApply.creator },
                        //                                                                keyword3 = new ExtWxSendMsgDataCore { value = entityApply.submitdt == null ? "" : ((DateTime)entityApply.submitdt).ToString("yyyy-MM-dd HH:mm:ss") },
                        //                                                                keyword4 = new ExtWxSendMsgDataCore { value = entityApply.rplbnm + entityApply.amt_apply.ToString() }
                        //                                                            });
                        //}
                        //#endregion
                        break;
                    default:throw new BizException("收付费标志有误");
                }
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFinaNullResultInfo SettlementConfirm(string _strMemberid, FnCore _collec)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if (_collec.optlb != GlobalSettings.ENTITYOPERATION_UPDATE) throw new Exception("操作标识符异常");
                FIN_FEENOTICE entityFnUpd = fcctx.FIN_FEENOTICE.SingleOrDefault(n => n.fnid == _collec.fnid
                                                                                                                            && (n.pid != "9"));//防止意外的多次结算 qm191227
                if (entityFnUpd == default(FIN_FEENOTICE)) throw new BizException("未找到结算单信息");
                if (string.Compare(entityFnUpd.pid, "3") < 0) throw new BizException("结算单状态有误。#" + _collec.fnid); //审批中
                List<FIN_FEENOTICE_DETAIL> lstDtl = fcctx.FIN_FEENOTICE_DETAIL.Where(n => n.fnid == _collec.fnid).ToList();
                decimal decAmtTotal = 0;
                foreach (FIN_FEENOTICE_DETAIL _entity in lstDtl) decAmtTotal += _entity.amt_settled;
                if (entityFnUpd.amt_settle != decAmtTotal) throw new BizException("结算单金额与实际结算金额不符");
                if (entityFnUpd.fntp == GlobalSettings.FNTP_BIZ)
                {
                    FIN_BILLTB entityBill = fcctx.FIN_BILLTB.SingleOrDefault(n => n.billid == entityFnUpd.billid);
                    if (entityBill == default(FIN_BILLTB)) throw new BizException("未找到结算单对应的账单信息");
                    if (string.Compare(entityBill.pid, "3") <= 0) throw new BizException("账单状态有误"); //审批完成
                    if (entityBill.rplb != entityFnUpd.rplb) throw new BizException("账单收付费标志有误");
                    //仅第一次结算确认时扣减账单预结金额、计算已结金额等
                    //其他情况（如欠发票的结算单结算）不再进行预结金额扣减等操作 qm191227
                    if (entityFnUpd.pid == "3")
                    {
                        entityBill.amt_pre -= entityFnUpd.amt_settle;
                        if (entityBill.rplb == GlobalSettings.FEELB_PAY)
                        {
                            entityBill.amt_pay_ed += entityFnUpd.amt_settle;
                            if (entityBill.amt_pay_ed == entityBill.amt_pay)
                            {
                                List<BIZ_FEE_DETAIL_fin> lstFeePay = fcctx.BIZ_FEE_DETAIL_fin.Where(n => n.billid == entityBill.billid).ToList();
                                foreach (BIZ_FEE_DETAIL_fin _entity in lstFeePay) _entity.pid = "9";
                                entityBill.pid = "9"; //结算完成
                            }
                        }
                        if (entityBill.rplb == GlobalSettings.FEELB_RECEIVE)
                        {
                            entityBill.amt_rec_ed += entityFnUpd.amt_settle;
                            if (entityBill.amt_rec_ed == entityBill.amt_rec)
                            {
                                List<BIZ_FEE_DETAIL_fin> lstFeeRec = fcctx.BIZ_FEE_DETAIL_fin.Where(n => n.billid == entityBill.billid).ToList();
                                foreach (BIZ_FEE_DETAIL_fin _entity in lstFeeRec) _entity.pid = "9";
                                entityBill.pid = "9"; //结算完成
                            }
                        }
                    }
                }
                List<FIN_INVOFNMAPPING> lstInvo = fcctx.FIN_INVOFNMAPPING.Where(n => n.fnid == entityFnUpd.fnid).ToList();
                decimal decAmtInvo = 0;
                foreach (FIN_INVOFNMAPPING _entity in lstInvo) decAmtInvo += _entity.amt_invo;
                if (decAmtInvo == entityFnUpd.amt_settle)
                {
                    entityFnUpd.settleid = _strMemberid;
                    entityFnUpd.settledt = DateTime.Now;
                    entityFnUpd.pid = "9"; //结算完成
                    respReturn.statusMsg = "操作成功。结算完成";
                }
                else
                {
                    entityFnUpd.pid = "4"; //结费完成(欠发票)
                    respReturn.statusMsg = "操作成功。结费完成(欠发票)";
                }
                fcctx.SubmitChanges();
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFinaNullResultInfo FnPrint(FnCore _collec)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                FIN_FEENOTICE entityFnUpd = fcctx.FIN_FEENOTICE.SingleOrDefault(n => n.fnid == _collec.fnid);
                if (entityFnUpd == default(FIN_FEENOTICE)) throw new BizException("未找到结算单信息");
                entityFnUpd.printlb = GlobalSettings.YESLB;
                fcctx.SubmitChanges();
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespLedger GetLedger(string _strMemberid,string _strComcd,int _iFnid)
        {
            RespLedger respReturn = new RespLedger();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<LedgerExtInfo> lst = fcctx.VIEW_FIN_FEENOTICE_DETAIL.Where(n => n.fnid == _iFnid).AsEnumerable()
                                                                                                            .Select(n => new LedgerExtInfo
                                                                                                            {
                                                                                                                amt_settled = n.amt_settled,
                                                                                                                fnid = n.fnid,
                                                                                                                createdt = n.createdt.ToString("yyyy-MM-dd HH:mm"),
                                                                                                                creator = n.creator,
                                                                                                                acc_pay = n.acc_pay,
                                                                                                                settledt = n.settledt.ToString("yyyy-MM-dd HH:mm"),
                                                                                                                acc_rec = n.acc_rec,
                                                                                                                fndtlid = n.fndtlid,
                                                                                                                optlb = GlobalSettings.ENTITYOPERATION_NONE
                                                                                                            }).ToList();
                respReturn.result = new ResultLedger { ledgerextlist = lst };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFinaNullResultInfo UpdateLedger(string _strMemberid,LedgerInfo _collec)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                FIN_FEENOTICE entityFn = fcctx.FIN_FEENOTICE.SingleOrDefault(n => n.fnid == _collec.fnid);
                if (entityFn == default(FIN_FEENOTICE)) throw new BizException("未找到结算单信息");
                if(entityFn.pid!="3") throw new BizException("结算单状态有误");
                if(_collec.amt_settled==0) throw new BizException("结算金额有误");
                //实收实付信息不自动计入账单的已收已付额度中，由结算单完结接口专门更新
                switch (_collec.optlb)
                {
                    #region 新增
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        decimal amt_settled_total=0;
                        foreach (FIN_FEENOTICE_DETAIL _entity in fcctx.FIN_FEENOTICE_DETAIL.Where(n => n.fnid == _collec.fnid)) amt_settled_total += _entity.amt_settled;
                        if ((entityFn.amt_settle < (amt_settled_total + _collec.amt_settled)&&entityFn.amt_settle>0)
                            || (entityFn.amt_settle > (amt_settled_total + _collec.amt_settled) && entityFn.amt_settle < 0))
                            throw new BizException("本次结算金额超限");
                        int iFndtlid = sysLogic.GetRefNo("FNDTL");
                        if (iFndtlid == 0) throw new BizException("获取流水号失败");
                        FIN_FEENOTICE_DETAIL entityIns = new FIN_FEENOTICE_DETAIL
                        {
                            amt_settled = _collec.amt_settled,
                            settledt = Convert.ToDateTime(_collec.settledt),
                            acc_pay = _collec.acc_pay ?? "",
                            acc_rec = _collec.acc_rec ?? "",
                            createdt = DateTime.Now,
                            createid = _strMemberid,
                            fndtlid = iFndtlid,
                            fnid = _collec.fnid
                        };
                        fcctx.FIN_FEENOTICE_DETAIL.InsertOnSubmit(entityIns);
                        break;
                    #endregion
                    #region 删除
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        FIN_FEENOTICE_DETAIL entityDel = fcctx.FIN_FEENOTICE_DETAIL.SingleOrDefault(n => n.fndtlid == _collec.fndtlid);
                        if (entityDel == default(FIN_FEENOTICE_DETAIL)) throw new BizException("未找到结算详细信息");
                        fcctx.FIN_FEENOTICE_DETAIL.DeleteOnSubmit(entityDel);
                        break;
                    #endregion
                    default: throw new Exception("操作标识符异常");
                }
                fcctx.SubmitChanges();
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }


        public static RespExpense GetExpense(string _strCreateid,string _strComcd, int _iPageSize, int _iCurrPage,int _iFnid,int _iCusid,string[] _strsPid,DateTime? _dtSdt,DateTime? _dtEdt,int _iBillid)
        {
            RespExpense respReturn = new RespExpense();
            bool hasNextPage = false;
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if (_iPageSize == 0) throw new BizException("每页显示条数不能为0");
                int iRecTotal, iPageCount;
                iRecTotal = fcctx.VIEW_FIN_EXPENSEINFO.Count(n => n.comcd == _strComcd
                                                                                            && (string.IsNullOrEmpty(_strCreateid) ? true : n.createid == _strCreateid)
                                                                                            && (_strsPid.Length == 0 ? true : _strsPid.Contains(n.pid))
                                                                                            && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                            && (_iFnid == 0 ? true : n.fnid == _iFnid)
                                                                                            && (_dtSdt == null ? true : n.createdt > _dtSdt)
                                                                                            && (_dtEdt == null ? true : n.createdt < _dtEdt)
                                                                                            && (_iBillid == 0 ? true : n.billid == _iBillid));
                if ((iRecTotal / _iPageSize - _iCurrPage) > 0) hasNextPage = true;
                else if ((iRecTotal / _iPageSize - _iCurrPage) == 0)
                {
                    if ((iRecTotal % _iPageSize) > 0) hasNextPage = true;
                }
                iPageCount = (iRecTotal == 0 ? 0 : iRecTotal / _iPageSize + 1);
                List<ExpenseExtInfo> lstExpenseExtInfo = fcctx.VIEW_FIN_EXPENSEINFO.Where(n => n.comcd == _strComcd
                                                                                            && (string.IsNullOrEmpty(_strCreateid) ? true : n.createid == _strCreateid)
                                                                                            && (_strsPid.Length == 0 ? true : _strsPid.Contains(n.pid))
                                                                                            && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                             && (_iFnid == 0 ? true : n.fnid == _iFnid)
                                                                                            && (_dtSdt == null ? true : n.createdt > _dtSdt)
                                                                                            && (_dtEdt == null ? true : n.createdt < _dtEdt)
                                                                                            && (_iBillid == 0 ? true : n.billid == _iBillid))
                                                                                            .OrderBy(n => n.billid)
                                                                                            .Take(_iPageSize * _iCurrPage).Skip(_iPageSize * (_iCurrPage - 1)).AsEnumerable()
                                                                                            .Select(n => new ExpenseExtInfo
                                                                                            {
                                                                                                amount = n.amt_settle,
                                                                                                amt_payprice = n.amt_payprice,
                                                                                                amt_paytotal = n.amt_paytotal,
                                                                                                amt_invo_total = n.amt_invo_total,
                                                                                                billid = n.billid,
                                                                                                billtp = n.billtp,
                                                                                                billtpnm = n.billtpnm,
                                                                                                creator = n.creator,
                                                                                                createdt = n.createdt.ToString("yyyy-MM-dd HH:mm"),
                                                                                                cusid = n.cusid,
                                                                                                cusnm = n.cusnm,
                                                                                                feeid = n.feeid,
                                                                                                fnid = n.fnid,
                                                                                                fnremark = n.remark,
                                                                                                pid = n.pid,
                                                                                                taxrate = n.taxrate,
                                                                                                invotp = n.invotp,
                                                                                                invotpnm = n.invotpnm,
                                                                                                num = n.num,
                                                                                                objdesc = n.objdesc,
                                                                                                optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                                pidnm = n.fnpidnm,
                                                                                                printlb = n.printlb,
                                                                                                revcaseid = n.revcaseid,
                                                                                                revcasenm = n.casenm,
                                                                                                settledt = (n.settledt == null ? "" : ((DateTime)n.settledt).ToString("yyyy-MM-dd HH:mm")),
                                                                                                settlenm = n.settlenm,
                                                                                                settletp = n.settletp,
                                                                                                settletpnm = n.settletpnm
                                                                                            }).ToList();
                respReturn.result = new ResultExpense { expenseextlist = lstExpenseExtInfo, hasnextpage = hasNextPage, pagecount = iPageCount, reccount = iRecTotal };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFinaNullResultInfo UpdateExpense(string _strMemberid,string _strComcd,ExpenseBaseInfo _collec)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                int iFnDetailCount;
                switch (_collec.optlb)
                {
                    #region 新增
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        int iCusid = _collec.cusid;
                        int iFnid = sysLogic.GetRefNo("FN");
                        int iBillid = sysLogic.GetRefNo("BILL");
                        int iFeeid = sysLogic.GetRefNo("FEE");
                        DateTime dtNow = DateTime.Now;
                        BIZ_FEE_DETAIL_fin entityFeeIns = new BIZ_FEE_DETAIL_fin
                        {
                            amt_payprice = _collec.amt_payprice,
                            amt_paytotal = _collec.amt_paytotal,
                            amt_recprice = 0,
                            amt_rectotal = 0,
                            autolb = GlobalSettings.NOLB,
                            billid = iBillid,
                            brand = "",
                            comcd = _strComcd,
                            createdt = dtNow,
                            createid = _strMemberid,
                            ctid = 0,
                            ctsubid = 0,
                            cusid = _collec.cusid,
                            feeid = iFeeid,
                            invotp = _collec.invotp,
                            model = "",
                            num = _collec.num,
                            objdesc = _collec.objdesc,
                            objtp = "",
                            pid = "2",
                            projdtlid = 0,
                            projid = 0,
                            purchasdt = dtNow,
                            remark = "",
                            rplb = GlobalSettings.FEELB_PAY,
                            spec = "",
                            taxrate = _collec.taxrate,
                            unit = ""
                        };
                        FIN_BILLTB entityBillIns = new FIN_BILLTB
                        {
                            amt_pay = _collec.amt_paytotal,
                            amt_pay_ed = 0,
                            amt_pre = _collec.amt_paytotal,
                            amt_rec = 0,
                            amt_rec_ed = 0,
                            billid = iBillid,
                            billtp = _collec.billtp,
                            billtp_inner = GlobalSettings.FNTP_ADM,
                            comcd = _strComcd,
                            createdt = dtNow,
                            createid = _strMemberid,
                            cusid = _collec.cusid,
                            invotp = _collec.invotp,
                            pid = "3",
                            remark = "",
                            revcaseid = 0,
                            rplb = GlobalSettings.FEELB_PAY,
                            taxrate = _collec.taxrate
                        };
                        FIN_FEENOTICE entityFnIns = new FIN_FEENOTICE
                        {
                            amt_settle = _collec.amt_paytotal,
                            billid = iBillid,
                            comcd = _strComcd,
                            createdt = dtNow,
                            createid = _strMemberid,
                            cusid = _collec.cusid,
                            fnid = iFnid,
                            fntp = GlobalSettings.FNTP_ADM,
                            invotp = _collec.invotp,
                            pid = "1",
                            printlb = GlobalSettings.NOLB,
                            remark = _collec.fnremark ?? "",
                            revcaseid = _collec.revcaseid,
                            rplb = GlobalSettings.FEELB_PAY,
                            settleid = "",
                            settletp = _collec.settletp,
                            taxrate = _collec.taxrate
                        };

                        fcctx.BIZ_FEE_DETAIL_fin.InsertOnSubmit(entityFeeIns);
                        fcctx.FIN_BILLTB.InsertOnSubmit(entityBillIns);
                        fcctx.FIN_FEENOTICE.InsertOnSubmit(entityFnIns);
                        break;
                    #endregion
                    #region 修改
                    case GlobalSettings.ENTITYOPERATION_UPDATE:
                        FIN_FEENOTICE entityFnUpd = fcctx.FIN_FEENOTICE.SingleOrDefault(n => n.fnid == _collec.fnid);
                        if (entityFnUpd == default(FIN_FEENOTICE)) throw new BizException("未找到结算单信息");
                        if (string.Compare(entityFnUpd.pid, "1") > 0) throw new BizException("结算单正在审核，无法修改");
                        FIN_BILLTB entityBillUpd = fcctx.FIN_BILLTB.SingleOrDefault(n => n.billid == entityFnUpd.billid);
                        if (entityBillUpd == default(FIN_BILLTB)) throw new BizException("未找到账单信息");
                        BIZ_FEE_DETAIL_fin entityFeeUpd = fcctx.BIZ_FEE_DETAIL_fin.SingleOrDefault(n => n.billid == entityFnUpd.billid);
                        if (entityFeeUpd == default(BIZ_FEE_DETAIL_fin)) throw new BizException("未找到费用信息");

                        if (_collec.cusid!=0 && _collec.cusid != entityFnUpd.cusid) entityFnUpd.cusid = entityBillUpd.cusid = entityFeeUpd.cusid = _collec.cusid;
                        if (_collec.amt_paytotal != 0 && _collec.amt_paytotal != entityFnUpd.amt_settle) entityFnUpd.amt_settle = entityBillUpd.amt_pay = entityBillUpd.amt_pre = entityFeeUpd.amt_paytotal = _collec.amt_paytotal;
                        if (_collec.taxrate != 0 && _collec.taxrate != entityBillUpd.taxrate) entityFnUpd.taxrate = entityBillUpd.taxrate = entityFeeUpd.taxrate = _collec.taxrate;
                        if (!string.IsNullOrEmpty(_collec.invotp) && _collec.invotp!= entityFnUpd.invotp) entityFnUpd.invotp = entityBillUpd.invotp = _collec.invotp;
                        if (!string.IsNullOrEmpty(_collec.objdesc) && _collec.objdesc != entityFeeUpd.objdesc) entityFeeUpd.objdesc = _collec.objdesc;
                        if (_collec.num != 0 && _collec.num != entityFeeUpd.num) entityFeeUpd.num = _collec.num;
                        if (_collec.amt_payprice != 0 && _collec.amt_payprice != entityFeeUpd.amt_payprice) entityFeeUpd.amt_payprice = _collec.amt_payprice;
                        if (!string.IsNullOrEmpty(_collec.billtp) && _collec.billtp != entityBillUpd.billtp) entityBillUpd.billtp = _collec.billtp;

                        entityFnUpd.settletp = string.IsNullOrEmpty(_collec.settletp) ? entityFnUpd.settletp : _collec.settletp;
                        entityFnUpd.revcaseid = _collec.revcaseid;
                        entityFnUpd.printlb = _collec.printlb == GlobalSettings.YESLB ? _collec.printlb : entityFnUpd.printlb;
                        entityFnUpd.remark = _collec.fnremark ?? entityFnUpd.remark;
                        break;
                    #endregion
                    #region 删除
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        FIN_FEENOTICE entityFnDel = fcctx.FIN_FEENOTICE.SingleOrDefault(n => n.fnid == _collec.fnid);
                        if (entityFnDel == default(FIN_FEENOTICE)) throw new BizException("未找到结算单信息");
                        if (entityFnDel.pid == "9"|| entityFnDel.pid == "3") throw new BizException("结算单已进入结算流程，无法删除");
                        iFnDetailCount = fcctx.FIN_FEENOTICE_DETAIL.Count(n => n.fnid == _collec.fnid);
                        if (iFnDetailCount > 0) throw new BizException("结算单已有实际结算记录，无法删除");
                        FIN_BILLTB entityBillDel = fcctx.FIN_BILLTB.SingleOrDefault(n => n.billid == entityFnDel.billid);
                        if (entityBillDel == default(FIN_BILLTB)) throw new BizException("未找到账单信息");
                        BIZ_FEE_DETAIL_fin entityFeeDel = fcctx.BIZ_FEE_DETAIL_fin.SingleOrDefault(n => n.billid == entityFnDel.billid);
                        if (entityFeeDel == default(BIZ_FEE_DETAIL_fin)) throw new BizException("未找到费用信息");

                        fcctx.FIN_FEENOTICE.DeleteOnSubmit(entityFnDel);
                        fcctx.FIN_BILLTB.DeleteOnSubmit(entityBillDel);
                        fcctx.BIZ_FEE_DETAIL_fin.DeleteOnSubmit(entityFeeDel);
                        break;
                    #endregion
                    default: throw new Exception("操作标识符异常");
                }
                fcctx.SubmitChanges();
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }


        public static RespInvoice GetInvoiceInfo(string _strCreateid,string _strComcd,int _iPageSize,int _iCurrPage,string _strPid,string _strInvotp,string _strInvoCatelog,string _strEndlb,string _strBuyerTaxcode,string _strBuyer,int _iCusid_buyer,int _iCusid_seller,int _iInvoid,string _strGenSdt,string _strGenEdt)
        {
            RespInvoice respReturn = new RespInvoice();
            bool hasNextPage = false;
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if (_iPageSize == 0) throw new BizException("每页显示条数不能为0");
                int iRecTotal, iPageCount;
                iRecTotal = fcctx.VIEW_FIN_INVOINFO.Count(n => n.comcd == _strComcd
                                                                    && (string.IsNullOrEmpty(_strCreateid) ? true : n.createid == _strCreateid)
                                                                    && (string.IsNullOrEmpty(_strPid) ? true : n.pid == _strPid)
                                                                    && (string.IsNullOrEmpty(_strInvotp) ? true : n.invotp == _strInvotp)
                                                                    && (string.IsNullOrEmpty(_strInvoCatelog) ? true : n.invocatelog == _strInvoCatelog)
                                                                    && (string.IsNullOrEmpty(_strEndlb) ? true : (_strEndlb == "Y" ? n.amt_total == n.amt_fn : n.amt_total > n.amt_fn))
                                                                    && (string.IsNullOrEmpty(_strBuyer) ? true : n.buyer == _strBuyer)
                                                                    && (_iCusid_buyer == 0 ? true : n.cusid_buyer == _iCusid_buyer)
                                                                    && (_iCusid_seller == 0 ? true : n.cusid_seller == _iCusid_seller)
                                                                    && (_iInvoid == 0 ? true : n.invoid == _iInvoid)
                                                                    && (string.IsNullOrEmpty(_strGenSdt) ? true : n.gendt >= Convert.ToDateTime(_strGenSdt))
                                                                    && (string.IsNullOrEmpty(_strGenEdt) ? true : n.gendt <= Convert.ToDateTime(_strGenEdt))
                                                                    && (string.IsNullOrEmpty(_strBuyerTaxcode) ? true : n.buyertaxcode == _strBuyerTaxcode));
                if ((iRecTotal / _iPageSize - _iCurrPage) > 0) hasNextPage = true;
                else if ((iRecTotal / _iPageSize - _iCurrPage) == 0)
                {
                    if ((iRecTotal % _iPageSize) > 0) hasNextPage = true;
                }
                iPageCount = (iRecTotal == 0 ? 0 : iRecTotal / _iPageSize + 1);
                List<InvoiceBaseInfo> lst = fcctx.VIEW_FIN_INVOINFO.Where(n => n.comcd == _strComcd
                                                                                            && (string.IsNullOrEmpty(_strCreateid) ? true : n.createid == _strCreateid)
                                                                                            && (string.IsNullOrEmpty(_strPid) ? true : n.pid == _strPid)
                                                                                            && (string.IsNullOrEmpty(_strInvotp) ? true : n.invotp == _strInvotp)
                                                                                            && (string.IsNullOrEmpty(_strInvoCatelog) ? true : n.invocatelog == _strInvoCatelog)
                                                                                            && (string.IsNullOrEmpty(_strEndlb) ? true : (_strEndlb == "Y" ? n.amt_total == n.amt_fn : n.amt_total > n.amt_fn))
                                                                                            && (string.IsNullOrEmpty(_strBuyer) ? true : n.buyer == _strBuyer)
                                                                                            && (_iCusid_buyer == 0 ? true : n.cusid_buyer == _iCusid_buyer)
                                                                                            && (_iCusid_seller == 0 ? true : n.cusid_seller == _iCusid_seller)
                                                                                            && (_iInvoid == 0 ? true : n.invoid == _iInvoid)
                                                                                            && (string.IsNullOrEmpty(_strGenEdt) ? true : n.gendt <= Convert.ToDateTime(_strGenEdt))
                                                                                            && (string.IsNullOrEmpty(_strGenSdt) ? true : n.gendt >= Convert.ToDateTime(_strGenSdt))
                                                                                            && (string.IsNullOrEmpty(_strBuyerTaxcode) ? true : n.buyertaxcode == _strBuyerTaxcode)).AsEnumerable()
                                                                                            .Select(n => new InvoiceBaseInfo
                                                                                            {
                                                                                                accesscode = n.accesscode,
                                                                                                amt_fn = n.amt_fn,
                                                                                                amt_notax = n.amt_notax,
                                                                                                amt_tax = n.amt_tax,
                                                                                                amt_total = n.amt_total,
                                                                                                buyer = n.buyer,
                                                                                                buyeraddress = n.buyeraddress,
                                                                                                buyerbanknm = n.buyerbanknm,
                                                                                                buyerbankno = n.buyerbankno,
                                                                                                buyertaxcode = n.buyertaxcode,
                                                                                                buyertel = n.buyertel,
                                                                                                checkcode = n.checkcode,
                                                                                                comcd = n.comcd,
                                                                                                cusid_buyer = n.cusid_buyer,
                                                                                                cusid_seller = n.cusid_seller,
                                                                                                drawer = n.drawer,
                                                                                                fiscalcode = n.fiscalcode,
                                                                                                gendt = (n.gendt == null ? "" : ((DateTime)n.gendt).ToString("yyyy-MM-dd HH:mm")),
                                                                                                invocatelog = n.invocatelog,
                                                                                                invoicecode = n.invoicecode,
                                                                                                invoid = n.invoid,
                                                                                                invono = n.invono,
                                                                                                invotp = n.invotp,
                                                                                                invotpnm = n.speccn,
                                                                                                optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                                payee = n.payee,
                                                                                                pid = n.pid,
                                                                                                relatedcode = n.relatedcode,
                                                                                                remark = n.remark,
                                                                                                reviewer = n.reviewer,
                                                                                                seller = n.seller,
                                                                                                selleraddress = n.selleraddress,
                                                                                                sellerbanknm = n.sellerbanknm,
                                                                                                sellerbankno = n.sellerbankno,
                                                                                                sellertaxcode = n.sellertaxcode,
                                                                                                sellertel = n.sellertel,
                                                                                                taxrate = n.taxrate,
                                                                                                validdt = (n.validdt == null ? "" : ((DateTime)n.validdt).ToString("yyyy-MM-dd HH:mm")),
                                                                                                validreason = n.validreason
                                                                                            }).ToList();
                respReturn.result = new ResultInvoice { hasnextpage = hasNextPage, involist = lst, pagecount = iPageCount, reccount = iRecTotal };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFinaNullResultInfo UpdateInvoiceInfo(string _strMemberid, string _strComcd,InvoiceBaseInfo _collec)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                int iDtlCount = 0;
                switch (_collec.optlb)
                {
                    #region 新增
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        int iInvoid = sysLogic.GetRefNo("INVO");
                        DateTime? dtNull = null;
                        FIN_INVOICETB entityIns = new FIN_INVOICETB
                        {
                            accesscode = "",
                            amt_fn = _collec.amt_fn,
                            amt_notax = _collec.amt_notax,
                            amt_tax = _collec.amt_tax,
                            amt_total = _collec.amt_total,
                            buyer = _collec.buyer ?? "",
                            buyeraddress = _collec.buyeraddress ?? "",
                            buyerbanknm = _collec.buyerbanknm ?? "",
                            buyerbankno = _collec.buyerbankno ?? "",
                            buyertaxcode = _collec.buyertaxcode ?? "",
                            buyertel = _collec.buyertel ?? "",
                            checkcode = _collec.checkcode ?? "",
                            comcd = _strComcd,
                            createdt = DateTime.Now,
                            createid = _strMemberid,
                            cusid_buyer = _collec.cusid_buyer,
                            cusid_seller = _collec.cusid_seller,
                            drawer = _collec.drawer ?? "",
                            fiscalcode = _collec.fiscalcode ?? "",
                            gendt = string.IsNullOrEmpty(_collec.gendt) ? dtNull : Convert.ToDateTime(_collec.gendt),
                            invocatelog = _collec.invocatelog,
                            invoicecode = _collec.invoicecode,
                            invoid = iInvoid,
                            invono = _collec.invono,
                            invotp = _collec.invotp,
                            payee = _collec.payee ?? "",
                            pid = string.IsNullOrEmpty(_collec.pid) ? "1" : _collec.pid,
                            relatedcode = _collec.relatedcode ?? "",
                            remark = _collec.remark ?? "",
                            reviewer = _collec.reviewer ?? "",
                            seller = _collec.seller ?? "",
                            selleraddress = _collec.selleraddress ?? "",
                            sellerbanknm = _collec.sellerbanknm ?? "",
                            sellerbankno = _collec.sellerbankno ?? "",
                            sellertaxcode = _collec.sellertaxcode ?? "",
                            sellertel = _collec.sellertel ?? "",
                            taxrate = _collec.taxrate,
                            validdt = dtNull,
                            validreason = ""
                        };
                        fcctx.FIN_INVOICETB.InsertOnSubmit(entityIns);
                        break;
                    #endregion
                    #region 修改
                    case GlobalSettings.ENTITYOPERATION_UPDATE:
                        iDtlCount = fcctx.FIN_INVOFNMAPPING.Count(n => n.invoid == _collec.invoid);
                        FIN_INVOICETB entityUpd = fcctx.FIN_INVOICETB.SingleOrDefault(n => n.invoid == _collec.invoid);
                        if (entityUpd == default(FIN_INVOICETB)) throw new BizException("未找到发票信息");
                        if (_collec.amt_total > 0 && _collec.amt_fn > 0 && _collec.amt_total < _collec.amt_fn) throw new BizException("票面金额不能小于已挂账金额");
                        entityUpd.buyer = (_collec.buyer == entityUpd.buyer ? entityUpd.buyer : _collec.buyer);
                        entityUpd.buyeraddress = (_collec.buyeraddress == entityUpd.buyeraddress ? entityUpd.buyeraddress : _collec.buyeraddress);
                        entityUpd.buyerbanknm = (_collec.buyerbanknm == entityUpd.buyerbanknm ? entityUpd.buyerbanknm : _collec.buyerbanknm);
                        entityUpd.buyerbankno = (_collec.buyerbankno == entityUpd.buyerbankno ? entityUpd.buyerbankno : _collec.buyerbankno);
                        entityUpd.buyertaxcode = (_collec.buyertaxcode == entityUpd.buyertaxcode ? entityUpd.buyertaxcode : _collec.buyertaxcode);
                        entityUpd.buyertel = (_collec.buyertel == entityUpd.buyertel ? entityUpd.buyertel : _collec.buyertel);
                        entityUpd.checkcode = (_collec.checkcode == entityUpd.checkcode ? entityUpd.checkcode : _collec.checkcode);
                        entityUpd.cusid_buyer = _collec.cusid_buyer == entityUpd.cusid_buyer ? entityUpd.cusid_buyer : _collec.cusid_buyer;
                        entityUpd.cusid_seller = _collec.cusid_seller == entityUpd.cusid_seller ? entityUpd.cusid_seller : _collec.cusid_seller;
                        entityUpd.drawer = (_collec.drawer == entityUpd.drawer ? entityUpd.drawer : _collec.drawer);
                        entityUpd.fiscalcode = (_collec.fiscalcode == entityUpd.fiscalcode ? entityUpd.fiscalcode : _collec.fiscalcode);
                        entityUpd.gendt = (string.IsNullOrEmpty(_collec.gendt) ? entityUpd.gendt : Convert.ToDateTime(_collec.gendt));
                        entityUpd.invocatelog = (string.IsNullOrEmpty(_collec.invocatelog) ? entityUpd.invocatelog : _collec.invocatelog);
                        entityUpd.invoicecode = (_collec.invoicecode == entityUpd.invoicecode ? entityUpd.invoicecode : _collec.invoicecode);
                        entityUpd.invono = (_collec.invono == entityUpd.invono ? entityUpd.invono : _collec.invono);
                        entityUpd.invotp = (string.IsNullOrEmpty(_collec.invotp) ? entityUpd.invotp : _collec.invotp);
                        entityUpd.payee = (_collec.payee == entityUpd.payee ? entityUpd.payee : _collec.payee);
                        entityUpd.remark = (_collec.remark == entityUpd.remark ? entityUpd.remark : _collec.remark);
                        entityUpd.reviewer = (_collec.reviewer == entityUpd.reviewer ? entityUpd.reviewer : _collec.reviewer);
                        entityUpd.seller = (_collec.seller == entityUpd.seller ? entityUpd.seller : _collec.seller);
                        entityUpd.selleraddress = (_collec.selleraddress == entityUpd.selleraddress ? entityUpd.selleraddress : _collec.selleraddress);
                        entityUpd.sellerbanknm = (_collec.sellerbanknm == entityUpd.sellerbanknm ? entityUpd.sellerbanknm : _collec.sellerbanknm);
                        entityUpd.sellerbankno = (_collec.sellerbankno == entityUpd.sellerbankno ? entityUpd.sellerbankno : _collec.sellerbankno);
                        entityUpd.sellertaxcode = (_collec.sellertaxcode == entityUpd.sellertaxcode ? entityUpd.sellertaxcode : _collec.sellertaxcode);
                        entityUpd.sellertel = (_collec.sellertel == entityUpd.sellertel ? entityUpd.sellertel : _collec.sellertel);
                        entityUpd.taxrate = _collec.taxrate == entityUpd.taxrate ? entityUpd.taxrate : _collec.taxrate;
                        if (iDtlCount == 0)
                        {
                            entityUpd.amt_notax = (_collec.amt_notax == entityUpd.amt_notax ? entityUpd.amt_notax : _collec.amt_notax);
                            entityUpd.amt_tax = (_collec.amt_tax == entityUpd.amt_tax ? entityUpd.amt_tax : _collec.amt_tax);
                            entityUpd.amt_total = (_collec.amt_total == entityUpd.amt_total ? entityUpd.amt_total : _collec.amt_total);
                        }

                        break;
                    #endregion
                    #region 删除
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        iDtlCount = fcctx.FIN_INVOFNMAPPING.Count(n => n.invoid == _collec.invoid);
                        if (iDtlCount > 0) throw new BizException("该发票已挂结算单，无法删除");
                        FIN_INVOICETB entityDel = fcctx.FIN_INVOICETB.SingleOrDefault(n => n.invoid == _collec.invoid && n.comcd == _strComcd);
                        if(entityDel==default(FIN_INVOICETB)) throw new BizException("未找到发票信息");
                        fcctx.FIN_INVOICETB.DeleteOnSubmit(entityDel);
                        break;
                    #endregion
                    default: throw new Exception("操作标识符异常");
                }
                fcctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFinaNullResultInfo InvoiceSettle(string _strMemberid,string _strComcd,int _iFnid,string _strOptlb, List<InvoiceSettleInfo> _lstInvoSettle)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                FIN_FEENOTICE entityFn = fcctx.FIN_FEENOTICE.SingleOrDefault(n => n.fnid == _iFnid && n.comcd == _strComcd);
                if (entityFn == default(FIN_FEENOTICE)) throw new BizException("未找到结算单信息");
                if (entityFn.pid == "9") throw new BizException("该联系单已结算完成。无法操作发票信息");
                List<FIN_INVOFNMAPPING> lstSettledInvo = fcctx.FIN_INVOFNMAPPING.Where(n => n.fnid == _iFnid).ToList();
                decimal decAmtSettled = 0;
                switch (_strOptlb)
                {
                    #region 新增
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        foreach (FIN_INVOFNMAPPING _entity in lstSettledInvo) { decAmtSettled += _entity.amt_invo; }
                        if (decAmtSettled >= entityFn.amt_settle) throw new BizException("结算金额已足额，无法添加结算发票");
                        List<FIN_INVOICETB> lstInvo = fcctx.FIN_INVOICETB.Where(n => _lstInvoSettle.Select(m => m.invoid).Contains(n.invoid)).ToList();
                        DateTime dtNow = DateTime.Now;
                        List<FIN_INVOFNMAPPING> lstSettleInvoNew = new List<FIN_INVOFNMAPPING>();
                        foreach (FIN_INVOICETB _entity in lstInvo)
                        {
                            InvoiceSettleInfo collec = _lstInvoSettle.SingleOrDefault(n => n.invoid == _entity.invoid);
                            if (collec == default(InvoiceSettleInfo)) continue;
                            if (_entity.pid != "1") throw new BizException("发票状态有误，无法结算");
                            if (_entity.amt_fn + collec.amt_invo > _entity.amt_total) throw new BizException("发票流水号【" + _entity.invoid + "】的可结算金额不足");
                            _entity.amt_fn += collec.amt_invo;
                            FIN_INVOFNMAPPING entityIns = new FIN_INVOFNMAPPING
                            {
                                amt_invo = collec.amt_invo,
                                invoid = collec.invoid,
                                createdt = dtNow,
                                createid = _strMemberid,
                                fnid = _iFnid
                            };
                            lstSettleInvoNew.Add(entityIns);
                        }
                        fcctx.FIN_INVOFNMAPPING.InsertAllOnSubmit(lstSettleInvoNew);
                        break;
                    #endregion
                    #region 删除
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        List<int> lstInvoMappingDel = new List<int>();
                        List<FIN_INVOICETB> lstSettleInvoUpd = fcctx.FIN_INVOICETB.Where(n => _lstInvoSettle.Select(m => m.invoid).ToList().Contains(n.invoid)).ToList();
                        foreach (FIN_INVOICETB _entity in lstSettleInvoUpd)
                        {
                            InvoiceSettleInfo collec = _lstInvoSettle.SingleOrDefault(n => n.invoid == _entity.invoid);
                            if (collec == default(InvoiceSettleInfo)) continue;
                            if (_entity.pid != "1") throw new BizException("发票状态有误，无法结算");
                            if (_entity.amt_fn - collec.amt_invo < 0) throw new BizException("发票流水号【" + _entity.invoid + "】的已结算金额不足");
                            _entity.amt_fn -= collec.amt_invo;
                            lstInvoMappingDel.Add(_entity.invoid);
                        }
                        fcctx.FIN_INVOFNMAPPING.DeleteAllOnSubmit(lstSettledInvo.Where(n => n.fnid == _iFnid && lstInvoMappingDel.Contains(n.invoid)));
                        break;
                    #endregion
                    default: throw new Exception("操作标识符异常");
                }
                fcctx.SubmitChanges();
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFinaNullResultInfo FnRelateInvo(string _strMemberid,string _strComcd,int _iInvoid, string _strOptlb, List<InvoiceSettleInfo> _lstFnRelate)
        {
            RespFinaNullResultInfo respReturn = new RespFinaNullResultInfo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                FIN_INVOICETB entityInvo = fcctx.FIN_INVOICETB.SingleOrDefault(n => n.invoid == _iInvoid && n.comcd == _strComcd);
                if (entityInvo == default(FIN_INVOICETB)) throw new BizException("未找到发票信息");
                if(entityInvo.pid!="1") throw new BizException("发票状态有误，无法结算");
                List<FIN_FEENOTICE> lstFn = fcctx.FIN_FEENOTICE.Where(n => _lstFnRelate.Select(m => m.fnid).Contains(n.fnid)).ToList();
                if(lstFn.Exists(n=>n.pid=="9")) throw new BizException("有联系单已结算完成。无法操作发票信息");
                switch (_strOptlb)
                {
                    #region 新增
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        if (entityInvo.amt_total == entityInvo.amt_fn) throw new BizException("发票结算金额已满，无法继续结算");
                        List<FIN_INVOFNMAPPING> lstFnRelated = fcctx.FIN_INVOFNMAPPING.Where(n => _lstFnRelate.Select(m => m.fnid).Contains(n.fnid)).ToList();
                        if (lstFnRelated.Count(n => n.invoid == _iInvoid) > 0) throw new BizException("有结算单已参与过此发票的结算，无法继续操作");
                        decimal decAmtSettled = 0;
                        decimal decAmtInvoSettled = 0;
                        DateTime dtNow = DateTime.Now;
                        List<FIN_INVOFNMAPPING> lstSettleInvoNew = new List<FIN_INVOFNMAPPING>();
                        foreach (InvoiceSettleInfo _collec in _lstFnRelate)
                        {
                            FIN_FEENOTICE entityFn = lstFn.SingleOrDefault(n => n.fnid == _collec.fnid);
                            if (entityFn == default(FIN_FEENOTICE)) throw new BizException("未找到流水号为【" + _collec.fnid + "】结算单信息");
                            //当前结算单已关联发票的总额+当前需要关联发票的金额不能大于结算单的结算金额
                            foreach (FIN_INVOFNMAPPING _entity in lstFnRelated.Where(n => n.fnid == _collec.fnid)) decAmtSettled += _entity.amt_invo;
                            if (decAmtSettled + _collec.amt_invo > entityFn.amt_settle) throw new BizException("流水号【" + _collec.fnid + "】结算单未关联发票的余额有误，请确认关联金额");
                            decAmtInvoSettled += _collec.amt_invo;
                            FIN_INVOFNMAPPING entityIns = new FIN_INVOFNMAPPING
                            {
                                amt_invo = _collec.amt_invo,
                                invoid = _iInvoid,
                                createdt = dtNow,
                                createid = _strMemberid,
                                fnid = _collec.fnid
                            };
                            lstSettleInvoNew.Add(entityIns);
                        }
                        if(entityInvo.amt_fn+decAmtInvoSettled>entityInvo.amt_total) throw new BizException("发票结算金额超限，无法结算");
                        entityInvo.amt_fn += decAmtInvoSettled;
                        fcctx.FIN_INVOFNMAPPING.InsertAllOnSubmit(lstSettleInvoNew);
                        break;
                    #endregion
                    #region 删除
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        List<FIN_INVOFNMAPPING> lstFnRelatedDel = fcctx.FIN_INVOFNMAPPING.Where(n => n.invoid == _iInvoid && _lstFnRelate.Select(m => m.fnid).Contains(n.fnid)).ToList();
                        decimal decAmtInvoSettledDel = 0;
                        foreach(FIN_INVOFNMAPPING _entity in lstFnRelatedDel) decAmtInvoSettledDel += _entity.amt_invo;
                        if (entityInvo.amt_fn - decAmtInvoSettledDel < 0) throw new BizException("发票流水号【" + entityInvo.invoid + "】的已结算金额不足");
                        entityInvo.amt_fn -= decAmtInvoSettledDel;
                        fcctx.FIN_INVOFNMAPPING.DeleteAllOnSubmit(lstFnRelatedDel);
                        break;
                    #endregion
                    default: throw new Exception("操作标识符异常");
                }
                fcctx.SubmitChanges();
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespFNInvo GetFNInvo(int _iInvoid)
        {
            RespFNInvo respReturn = new RespFNInvo();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                respReturn.result = new ResultFNInvo
                {
                    fninvolist = fcctx.VIEW_FIN_FNINVO.Where(n => n.invoid == _iInvoid).AsEnumerable().Select(n => new FNInvo
                    {
                        amt_involeft = n.amt_settle - n.amt_invosum,
                        amt_invosum = n.amt_invosum,
                        amt_settle = n.amt_settle,
                        createdt = n.createdt.ToString("yyyy-MM-dd HH:mm"),
                        createid = n.createid,
                        creator = n.creator,
                        cusid = n.cusid,
                        cusnm = n.cusnm,
                        fnid = n.fnid,
                        fntp = n.fntp,
                        invoid = n.invoid,
                        pid = n.pid,
                        pidnm = n.pidnm,
                        printlb = n.printlb,
                        taxrate = n.taxrate
                    }).ToList()
                };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespInvoFN GetInvoFN(int _iFnid)
        {
            RespInvoFN respReturn = new RespInvoFN();
            FinancialDataContext fcctx = new FinancialDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                respReturn.result = new ResultInvoFN
                {
                    invofnlist = fcctx.VIEW_FIN_INVOFN.Where(n => n.invoid == _iFnid).AsEnumerable().Select(n => new InvoiceFN
                    {
                        accesscode = n.accesscode,
                        amt_fn = n.amt_fn,
                        amt_notax = n.amt_notax,
                        amt_tax = n.amt_tax,
                        amt_total = n.amt_total,
                        buyer = n.buyer,
                        buyeraddress = n.buyeraddress,
                        buyerbanknm = n.buyerbanknm,
                        buyerbankno = n.buyerbankno,
                        buyertaxcode = n.buyertaxcode,
                        buyertel = n.buyertel,
                        checkcode = n.checkcode,
                        comcd = n.comcd,
                        cusid_buyer = n.cusid_buyer,
                        cusid_seller = n.cusid_seller,
                        drawer = n.drawer,
                        fiscalcode = n.fiscalcode,
                        fnid = n.fnid,
                        gendt = n.gendt == null ? "" : ((DateTime)n.gendt).ToString("yyyy-MM-dd HH:mm"),
                        invocatelog = n.invocatelog,
                        invoicecode = n.invoicecode,
                        invoid = n.invoid,
                        invono = n.invono,
                        invotp = n.invotp,
                        invotpnm = n.speccn,
                        optlb = GlobalSettings.ENTITYOPERATION_NONE,
                        payee = n.payee,
                        pid = n.pid,
                        relatedcode = n.relatedcode,
                        remark = n.remark,
                        reviewer = n.reviewer,
                        seller = n.seller,
                        selleraddress = n.selleraddress,
                        sellerbanknm = n.sellerbanknm,
                        sellerbankno = n.sellerbankno,
                        sellertaxcode = n.sellertaxcode,
                        sellertel = n.sellertel,
                        taxrate = n.taxrate,
                        validdt = n.validdt == null ? "" : ((DateTime)n.validdt).ToString("yyyy-MM-dd HH:mm"),
                        validreason = n.validreason
                    }).ToList()
                };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (fcctx.Connection.State != ConnectionState.Closed) fcctx.Connection.Close(); }
            return respReturn;
        }

    }
}